S
S
semax952016-03-12 02:31:12
Android
semax95, 2016-03-12 02:31:12

How to send http request in android?

Hello everyone, after a long search, I have a question, in the latest Android studio I don’t see the httpClient methods, etc.

public static String GET(String url){
        InputStream inputStream = null;
        String result = "";
        try {
 
            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();
 
            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
 
            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();
 
            // convert inputstream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";
 
        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }
 
        return result;
    }

half of the methods from this section of code are not imported.
Googled, but the code is the same everywhere...
screenshot from android studio
270906e8a9cd493e92ad5e93f587ed74.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Dovnar, 2016-03-12
@semax95

Removed HTTPClient from Android version 6 .
Try to write in build.gradle:

android {
    useLibrary 'org.apache.http.legacy'
}

I
illuzor, 2016-03-13
@iLLuzor

Or you can use specialized libraries like okhttp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question