45

With the release of Android 5.1, it looks like all the Apache http stuff has been deprecated. Looking at the documentation is useless; they all say

This class was deprecated in API level 22. Please use openConnection() instead. Please visit this webpage for further details.

Which is fine the first time you read it, but when EVERY class that was deprecated says that, it's not that helpful.

Anyway, what are the alternatives for classes like HttpEntity, specifically StringEntity, and MultipartEntity? I substituted BasicNameValuePair for my own implementation of Android's Pair<T, S> class, and it looks like URLEncoder.encode is a good substitute for URLEncodedUtils, but I'm not sure what to do about HttpEntity.

EDIT

I've decided just to re-write the networking stuff. Gonna try to use Retrofit and OkHttp

EDIT

Seriously take a look at switching your calls and stuff to Retrofit. Pretty nifty. I'm glad I did. There were a few hurdles, but it's cool.

4
  • You can have a look at how to switch from HttpClient to HttpUrlConnection
    – hgoebl
    Mar 20, 2015 at 7:33
  • If any of the answers below helped you out, you should accent the most helpful one. Mar 24, 2015 at 15:18
  • Using OkHttp is wise choice. Mar 30, 2015 at 20:28
  • HttpUrlConnection is the new standard (no external libraries required) way to do it. See this answer for a good example implementing a json interface. The json can be stripped fairly easily if not needed. I had a horrible time getting Retrofit to work with Proguard and gave up. Jul 27, 2016 at 20:25

3 Answers 3

21

You can always import the last Apache Http client and use that. Also, you might want to take a look at a networking library like Volley or Retrofit, just in case you can use that instead. If starting a new project, using a networking library is recommended because there is no need to reinvent the wheel. But if you are stuck with using HttpClient, then read on.

EDIT: Latest news on Apache HttpClient (as of 11/07/2015)

Google Android 1.0 was released with a pre-BETA snapshot of Apache HttpClient. To coincide with the first Android release Apache HttpClient 4.0 APIs had to be frozen prematurely, while many of interfaces and internal structures were still not fully worked out. As Apache HttpClient 4.0 was maturing the project was expecting Google to incorporate the latest code improvements into their code tree. Unfortunately it did not happen. Version of Apache HttpClient shipped with Android has effectively become a fork. Eventually Google decided to discontinue further development of their fork while refusing to upgrade to the stock version of Apache HttpClient citing compatibility concerns as a reason for such decision. As a result those Android developers who would like to continue using Apache HttpClient APIs on Android cannot take advantage of newer features, performance improvements and bug fixes. Apache HttpClient 4.3 port for Android was intended to remedy the situation by providing official releases compatible with Google Android. Given that as of Android API 23 Google's fork of HttpClient has been removed this project has been discontinued.

However, there is an official Android port of the Apache HttpClient v4.3

Android API 22 and older should use the Apache HttpClient v4.3

dependencies {
         compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' 
}

Android API 23 and newer should use Apache HttpClient packages for Android maintained by Marek Sebera

dependencies {
     compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1' 
}

Info taken from Apache.org

2
  • 1
    As it wasn't obvious to me: You also have to change import org.apache.http.whatever to import cz.msebera.android.httpclient.whatever.
    – Timmmm
    Feb 18, 2016 at 9:40
  • It seems 'cz.msebera.android' httpclient is also deprecated now? When trying to build APK Android Studio fails and alerts that httpclient now conflicts with classes provided by Android
    – Nom1fan
    Jan 7, 2018 at 6:55
12

The HttpClient documentation points you in the right direction:

org.apache.http.client.HttpClient:

This interface was deprecated in API level 22. Please use openConnection() instead. Please visit this webpage for further details.

means that you should switch to java.net.URL.openConnection().

Here's how you could do it:

java.net.URL url = new java.net.URL("http://some-server");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");

// read the response
System.out.println("Response Code: " + conn.getResponseCode());
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
System.out.println(response);
2
  • you might be right for http client part ... the comment is absolutely ridiculous for the http server part ( or is URL.openConnection now able to serve too?! ;-) Jun 14, 2015 at 5:32
  • 2
    If you read This URL. android-developers.blogspot.in/2011/09/… Which client is best? Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases. For Gingerbread and better, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where we will be spending our energy going forward.
    – hims_3009
    Jul 20, 2015 at 5:49
2

Call webservice httpclient replace by httpurlconnection

my code here

public static String getDataFromUrl(String url) {
        String result = null;
//        System.out.println("URL comes in jsonparser class is:  " + url);
        try {
            URL myurl=new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection) myurl
                    .openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoInput(true);
            urlConnection.connect();
            InputStream is=urlConnection.getInputStream();
            if (is != null) {
                StringBuilder sb = new StringBuilder();
                String line;
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is));
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }
                    reader.close();
                } finally {
                  is.close();
                }
                result = sb.toString();
            }
        }catch (Exception e){
            result=null;
        }
        return result;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.