How to Use the Legacy Apache Http Client on Android Marshmallow

How to add Apache HTTP API (legacy) as compile-time dependency to build.grade for Android M?

For API 23:

Top level build.gradle - /build.gradle

buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
}
}
...

Module specific build.gradle - /app/build.gradle

android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
useLibrary 'org.apache.http.legacy'
...
}

Official docs (for preview though): http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

Latest android gradle plugin changelog: http://tools.android.com/tech-docs/new-build-system

Apache HTTP connection with Android 6.0 (Marshmallow)

This page discusses the removal of the Apache HTTP classes, and it suggests a workaround as well:

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'
}

In my case Android Studio still complained that it couldn't find these classes, but the app did build and run.

The page does recommend you move to HttpURLConnection, though.

What are the implications of the removal of HTTPClient in android M

As per documentation here

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'
}

So basically HTTP client was replaced by HttpURLConnection class because it is faster and consume less battery power.
Which DOES NOT mean that you can not use volley,retrofit etc.

How to use Legacy Apache in Eclipse in order to support Android M

Find org.apache.http.legacy.jar which is in Android/Sdk/platforms/android-23/optional, add it to your dependency.

Using org.appache.http.client in android studio

Android 6.0 release removes support for the Apache HTTP client you can use HttpURLConnection instead

or you can add

useLibrary  'org.apache.http.legacy' 

in your build.gradle for target sdk 23

android{
compileSdkVersion 23
buildToolsVersion '23.0.1'
useLibrary 'org.apache.http.legacy'
}

see this link



Related Topics



Leave a reply



Submit