Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Android has deprecated the Apache module since API level 22, so my question is, how do I use, for example HttpResponse from the Apache library, not from Android SDK? The problem is that the're the same in both packages.

But, for example, HttpGet is OK, because it's called HttpGetHC4 in Apache.

like image 780
Evgeniy Avatar asked Mar 27 '15 06:03

Evgeniy


2 Answers

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.

like image 123
vincent091 Avatar answered Sep 20 '22 21:09

vincent091


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.

like image 43
Jehy Avatar answered Sep 18 '22 21:09

Jehy