Android ilovasi orqali qanday qilib http so`rov yuborish mumkin?

0 ovoz
98 marta ko‘rilgan
so‘radi 12 aprel chessmaster (3 bal)
Eclipseda Android uchun serverga ulana oladigan ilova yaratmoqchi edim. Ammo http so`rov yuborishning sira iloji bo`lmayapti. Internetda juda ko`p usullarni ko`rdim, ammo birontasi ham ish bermayapti.
izoh qoldirdi 13 aprel DelphiGuru (766 bal)
Android dastur yozayapsizmi? Android dasturizdan serverga http so'rov yubormoqchimisiz?

2 Javoblar

0 ovoz
javob berdi 13 aprel DelphiGuru (766 bal)

Agar Android dastur yozayotgan bo'lsangiz va undan http so'rov yubormoqchi bo'lsangiz, birinchi navbatda, dasturga INTERNET ruxsatini berish kerak https://developer.android.com/reference/android/Manifest.permission.html#INTERNET

Keyin, newRequestQueue dan foydalanishingiz mumkin:  

final TextView mTextView = (TextView) findViewById(R.id.text);
// ...

// RequestQueue ni initsilizatsiya qilish
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// url o'zgaruvchisi bilan so'rov yuborish
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});

// RequestQueuega so'rovni qo'shish
queue.add(stringRequest);

Quyidagi manbadan ko'proq ma'lumot olishiz mumkin : https://developer.android.com/training/volley/simple.html#simple

Android dastur yozishga, Android Studiodan foydalanishni maslahat beraman.

0 ovoz
javob berdi 25 aprel Mz~ (5 bal)
tahrirlandi 25 aprel Saidolim

DelphiGuru ning javobiga qo'shilgan xolda javob berishga xarakat qilamiz ))

private String ReadFunction(String requestUrl) {   //  requestUrl = Server Address

// URL yaratish

URL url = createUrl(requestUrl);

String strResult = null;
        try {
            strResult = HttpRequest(url);
        } catch (IOException e) {
            Log.e(LOG_TAG,  "Problem making the HTTP request.", e);  
        }

}

// Log ->  English  https://developer.android.com/studio/debug/am-logcat.html .

// Log -> Russian http://startandroid.ru/ru/uroki/vse-uroki-spiskom/19-urok-12-logi-i-vsplyvajuschie-soobschenija.html

// URL yaratish funksiyasi

private  URL createUrl(String stringUrl) {
        URL url = null;
        try {
            url = new URL(stringUrl);
        } catch (MalformedURLException e) {
            Log.e(LOG_TAG, "Problem building the URL ", e);
        }
        return url;
    }

// Serverga so'rov yuborish funksiyasi 

private String HttpRequest(URL url) throws IOException {
        String str = "";

        // Agar url null bo'lsa return bo'ladi va funksiya ishlamidi
        if (url == null) {
            return jsonResponse;
        }

        HttpURLConnection urlConnection = null;
        InputStream inputStream = null;
        try {
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(10000 /* milliseconds */);
            urlConnection.setConnectTimeout(15000 /* milliseconds */);
            urlConnection.setRequestMethod("GET");   // agar siz serverga ma'lumot yubormoqchi bo'lsangiz POST metod ni ishlatasiz ...
            urlConnection.connect();

            //  agar so'rov muvafafaqiyatli amalga oshsa server tomondan 200 code keladi. *** Android app --> Serverga ulandi **** 
            // keyin server tomondan kelayotgan ma'lumotni o'qishimiz mumkin boladi.
            if (urlConnection.getResponseCode() == 200) {   // tekshiramiz
                inputStream = urlConnection.getInputStream();
                str = readStream(inputStream);  // readStream funksiyasi orqali ma'lumotni o'qiymiz.
            } else {
                Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode()); // Server tomondan kelga codni tekshirishiz mumkin. Bu juda muhim, Server 404, 505, 200 va ... code junatish mumkin buni sababini google qilasiz :)
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (inputStream != null) {               
                inputStream.close();
            }
        }
        return str;
    }


// ReadFunksiyasi 

private String readFromStream(InputStream inputStream) throws IOException {
        StringBuilder output = new StringBuilder();
        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line = reader.readLine();
            while (line != null) {
                output.append(line);
                line = reader.readLine();
            }
        }
        return output.toString();  // Server tomondan kelgan *data
    }

Endi String ReadFunction(String requestUrl) funksiyasini Activitiyizda ishlatasiz faqat asosiy potokda (Stream) da emas.

for example  

String resultStr =  ReadFunction(www.yordam.uz)   //  resultStr  = Server tomondan kelgan *data

Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.

Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin.

Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

Telegram kanal YordamUzRss

...