12

I'm testing Volley's HurlStack in Android M Developer Preview.

After I change compileSdkVersion from 22 to 'android-MNC', all classes from org.apache.http are not compiled:

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;

How can I modify my code to solve this problem?

I know there're some changes related to Apache HTTP client, but it still doesn't work when I follow the steps to add useLibrary 'org.apache.http.legacy' in gradle.

Reference: HurlStack.java AOSP

Behavior Changes: Apache HTTP Client Removal

Song
  • 504
  • 7
  • 17
  • Apache HttpClient has been deprecated for a while. Volley uses HttpClient on old versions of Android, URLConnection on new versions. It may well be that they have removed httpclient from M preview alltogether. The error should not occur when you set your minimum version of android to a level where Volley uses URLConnection. – Christine Jul 10 '15 at 22:37
  • 1
    or you can include httpClient yourself. Like from here: https://code.google.com/p/httpclientandroidlib/ – Christine Jul 10 '15 at 22:38
  • @Christine Thank you for your answer. From https://developer.android.com/preview/setup-sdk.html section "Update an existing project", Google suggests the minSdkVersion should be set to 'MNC'. I wonder if there's an official way to bypass this. – Song Jul 13 '15 at 18:47
  • Volley is in your IDE as a source project. You can remove the call to httpclient there. The code is in "newRequestQueue()" in Volley.java – Christine Jul 13 '15 at 18:53
  • It seems like the app can be run on Developer Preview 2. I added useLibrary 'org.apache.http.legacy' and changed the gradle version to 1.3.0-beta4. IDE still cannot resolve the library though. Hope this helps. – Song Jul 13 '15 at 21:44

4 Answers4

4

The official “Behaviour Changes” document states that the Apache HTTP client is removed in Android M — not deprecated, but removed. Personally I highly suggest switching to OkHttp which actually is used as a HttpURLConnection engine since KitKat, by using a dependency you get all fresh goodies from Square team directly.

1

You can ignore these warns, because Volley is still compiled using API 22: https://github.com/mcxiaoke/android-volley/blob/master/gradle.properties

add these in proguard configuration: -dontwarn org.apache.http.** -dontwarn com.android.volley.toolbox.**

cn123h
  • 2,302
  • 1
  • 21
  • 16
0

Apart from using okHttp, the fallback is to use legacy apache httpclient as suggested by Google. See my answer here to get it working.

How to use the legacy Apache HTTP client on Android Marshmallow?

Community
  • 1
  • 1
WenChao
  • 3,586
  • 6
  • 32
  • 46
0

If, like me, the only reason you were including HttpClient is because you were testing HTTP response codes:

if (error.networkResponse.statusCode == HttpStatus.SC_UNAUTHORIZED) {}

then a simple fix is just to use the version of the constants that are in the HttpURLConnection class:

if (error.networkResponse.statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) {}
Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
  • But note that not all the Http status codes are present in the HttpURLConnection class. For instance 307 for a temporary redirect is not present anymore. – Mike Nov 20 '15 at 17:08