Android deprecated apache module (HttpClient, HttpResponse, etc.) Android deprecated apache module (HttpClient, HttpResponse, etc.) android android

Android deprecated apache module (HttpClient, HttpResponse, etc.)


The method HttpClient was deprecated. You can now use the URLConnection as you can see in this example:

private StringBuffer request(String urlString) {    // TODO Auto-generated method stub    StringBuffer chaine = new StringBuffer("");    try{        URL url = new URL(urlString);        HttpURLConnection connection = (HttpURLConnection)url.openConnection();        connection.setRequestProperty("User-Agent", "");        connection.setRequestMethod("POST");        connection.setDoInput(true);        connection.connect();        InputStream inputStream = connection.getInputStream();        BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));        String line = "";        while ((line = rd.readLine()) != null) {            chaine.append(line);        }    }    catch (IOException e) {        // Writing exception to log        e.printStackTrace();    }    return chaine;}

I hope this is helping someone.


There is nothing bad with using Apache's modules. Google just made a big mess of it, because they failed to make a successful fork. Google and Apache integration was supervised by Jesse Wilson - he worked in Google, messed up everything and then made his own library (OkHttp) during work in square. That's a really ugly story.

I advice against using legacy the JAR file because it contains an old version of Apache libraries without improvements and bugfixes (it is a very old pre-BETA snapshot). As you can see on the official page of Apache components, there is a fresh 4.4 version, compatible with all of Android versions. It just had to be repackaged under different namespace to avoid collision with old version.

You simply can add the dependency from Maven (or download release from GitHub):

dependencies {    compile "cz.msebera.android:httpclient:4.4.1.2"}

And then replace org.apache.http with cz.msebera.android.httpclient, so your imports will look like:

import cz.msebera.android.httpclient.Header;import cz.msebera.android.httpclient.HttpHost;import cz.msebera.android.httpclient.HttpResponse;

Yeah, you can continue using Apache libraries without wasting hours of rewriting of working code!

As for the difference between using Apache components and HttpURLConnection, HttpURLConnection uses responce caching... And that's all. I'm not sure that you really want it and also you can always implement it yourself.

By the way, I tried alternatives like HttpURLConnection - those are not even close to Apache's power and simplicity.


In Android Marshmallow (sdk 23), you can add:

useLibrary 'org.apache.http.legacy'

to build.gradle in the android {} section as a workaround. This seems to be necessary for some of Google's own gms libraries!