8

After adding following changes in by build.gradle right after updating to latest android API level 23 (Marshmallow) all org.apache.http classes is not working.

android { compileSdkVersion 23 buildToolsVersion "23.0.0" defaultConfig { applicationId "com.myapp.package" minSdkVersion 15 targetSdkVersion 23 versionCode 2 versionName "1.1" } } 

I checked "Android API Differences Report" here. Its says all Classes of org.apache.http has been removed. Can someone suggest what is the alternative ?

Here is my code :

try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(address); httpPost.setEntity(new StringEntity("{\"longUrl\":\""+longUrl+"\"}")); httpPost.setHeader("Content-Type", "application/json"); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 
1
  • HttpClient was already deprecated in Android L, so i recommend using HttpURLConnection Commented Aug 25, 2015 at 11:15

3 Answers 3

16

See the Behavior Changes at Android Developers where it says that:

Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption. To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:

android { useLibrary 'org.apache.http.legacy' } 
Sign up to request clarification or add additional context in comments.

3 Comments

Gradle does not seem to recognize 'useLibrary'. Error below : Gradle DSL method not found: 'useLibrary()'
@techierishi: Make sure that you are on version 1.3.0 of the Gradle for Android plugin.
I've added useLibrary but I'm still getting in this "Unable to find optional library: org.apache.http.legacy" error in my build?
0

Please refer this. Alternative class is mentioned in Android Developer site.

The org.apache.http classes and the android.net.http.AndroidHttpClient class have been deprecated in Android 5.1. These classes are no longer being maintained and you should migrate any app code using these APIs to the URLConnection classes as soon as possible.

Comments

0

You can manually add an up-to-date version of the Apache HttpClient:

implementation 'org.apache.httpcomponents:httpclient:4.5.5'

Likewise, you can use another HTTP library altogether like OkHttp. You can also use a library which is more higher level, like Retrofit.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.