Apache Http Connection with Android 6.0 (Marshmallow)

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.

HTTP not working in Android 6.0

You can not use HttpPost or Httpclient in android 6.0.
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.

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.

I want to send Http request in marshmallow android?

public static Boolean excutePost(String targetURL, JSONObject jsonParam)
{
URL url;
HttpURLConnection connection = null;
try {
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST"); // hear you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc.
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // here you are setting the `Content-Type` for the data you are sending which is `application/json`
connection.connect();

//Send request
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream ());
wr.writeBytes(jsonParam.toString());
wr.flush();
wr.close ();

InputStream is;
int response = connection.getResponseCode();
if (response >= 200 && response <=399){
//return is = connection.getInputStream();
return true;
} else {
//return is = connection.getErrorStream();
return false;
}

} catch (Exception e) {

e.printStackTrace();
return false;

} finally {

if(connection != null) {
connection.disconnect();
}
}
}

AndroidHttpClient and HttpGet API deprecated in Android 6.0 Marshmallow (API 23)

The entire Apache API was removed from SDK 23 since from SDK 22 it was already deprecated.

You can see it here: http://developer.android.com/sdk/api_diff/23/changes.html

You can use Retrofit or Volley to reimplement your network requests.

org.apache.http.entity.FileEntity is deprecated in Android 6 (Marshmallow)

If you change your compileSdkVersion to 21, your app will compile cleanly. That being said, there are reasons why Google is backing away from the built-in HttpClient implementation, so you probably should pursue some other library. That
"some other library" could be:

  • the built-in classic Java HttpUrlConnection, though as you have found, its API leaves something to be desired
  • Apache's independent packaging of HttpClient for Android
  • OkHttp (my recommendation)
  • AndroidAsync

In particular, OkHttp seems to have a pretty good API for posting a file and posting a multipart form, which should be similar to what your HttpClient code is doing.

ParseApacheHttpClient issue with Android Marshmallow: can't find referenced method SSLSocketFactory.getHttpSocketFactory

This issue is solved in 2 parts:

  1. adding back the legacy Apache library
  2. updating ProGuard configuration for Parse.com

I'm going to address both parts below for completeness.

For those looking for the quick fix, the solution is to add the following line to your ProGuard configuation file:

-dontwarn android.net.SSLCertificateSocketFactory

1 - Apache Library

Android-M removes support for the Apache HTTP library. As mentioned in the question, the fix is to use the legacy support JAR by updating your build.gradle. Here is the code with a bit more context:

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

2 - Update ProGuard Configuration

The actual error comes from ProGuard, when it is trying to process Parse.com.

Warning: com.parse.ParseApacheHttpClient: 
can't find referenced method 'org.apache.http.conn.ssl.SSLSocketFactory getHttpSocketFactory(int,android.net.SSLSessionCache)'
in library class android.net.SSLCertificateSocketFactory

Parse is now open source, so checking the issues gives this page on GitHub:

  • Issue with android M compilesdkversion 23 #231

The discussion explains that this is just a ProGuard config item, and can be safely fixed by updating your config as per the related issue:

  • Update proguard-rules.pro for android-23 #234

Here is the change (line 3 fixes our issue, line 4 fixes related issue):

-keepattributes *Annotation*
-keepattributes Signature
-dontwarn android.net.SSLCertificateSocketFactory
-dontwarn android.app.Notification
-dontwarn com.squareup.**
-dontwarn okio.**

As explained in this comment, it is safe to ignore the warnings since those classes are only ever used on Android versions that still contain the mentioned classes.



Related Topics



Leave a reply



Submit