Volley in Android M developer preview: org.apache.http cannot be resolved
Asked Answered
C

4

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

Claudeclaudel answered 10/7, 2015 at 21:48 Comment(5)
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.Mares
or you can include httpClient yourself. Like from here: code.google.com/p/httpclientandroidlibMares
@Mares Thank you for your answer. From 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.Claudeclaudel
Volley is in your IDE as a source project. You can remove the call to httpclient there. The code is in "newRequestQueue()" in Volley.javaMares
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.Claudeclaudel
S
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.

Sferics answered 4/8, 2015 at 21:27 Comment(0)
Y
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.**

Yelena answered 4/10, 2015 at 9:37 Comment(0)
P
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?

Pastoralize answered 5/8, 2015 at 6:49 Comment(0)
K
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) {}
Kermanshah answered 20/11, 2015 at 17:0 Comment(1)
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.Hymanhymen

© 2022 - 2024 — McMap. All rights reserved.