如何在 Android M 上使用传统的 Apache HTTP 客户端?

背景资料

从 Android M 上看,Google 已经完全取消了对 Apache HTTP 客户端的支持(链接 这里) ,因为与其他替代方案相比,它的性能并不好。

这可能也是许多应用程序在 Android M 上崩溃的原因。

问题是

Google 允许你继续使用这个 API,只不过不是作为一个内置的 API,方法是将这一行添加到 gradle 文件中:

useLibrary 'org.apache.http.legacy'

所以,这就是我所做的:

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

还有:

android {
compileSdkVersion 'android-MNC'
buildToolsVersion "23.0.0 rc3"
useLibrary 'org.apache.http.legacy'


defaultConfig {
applicationId "com.example.user.androidmtest"
minSdkVersion 'MNC'
targetSdkVersion 'MNC'
versionCode 1
versionName "1.0"
}

当我尝试它时,它编译得很好(没有显示错误,我可以运行概念验证应用程序,因为它没有任何特殊的代码) ,但是当我尝试使用一些我知道是旧 API 的一部分的类(比如“ HttpClient”类)时,我发现它不允许我这样做。

我知道不推荐使用这个解决方案,但是我们必须让应用程序至少暂时在那里工作,直到我们100% 地处理所有应该改变的 Android M,我们不希望出现意外的崩溃。

下面是一个截图:

enter image description here

那个问题

为什么会这样? 我用对了吗?


编辑: 这里报道了这个问题:

Https://code.google.com/p/android/issues/detail?id=181474

94962 次浏览

Perfect solution here by running a simple file path check. by running

   android {
compileSdkVersion 'android-MNC'
buildToolsVersion "23.0.0 rc3"
useLibrary 'org.apache.http.legacy'


defaultConfig {
applicationId "com.example.user.androidmtest"
minSdkVersion 'MNC'
targetSdkVersion 'MNC'
versionCode 1
versionName "1.0"


}


getBootClasspath().each{File file ->
println file.absolutePath
}
}
}

You will get something like below

/Users/"yourname"/Development/android-sdk-macosx/platforms/android-MNC/android.jar /Users/"yourname"/Development/android-sdk-macosx/platforms/android-MNC/optional/org.apache.http.legacy.jar

So there you go, the jar is there.For some reason it didn't get added to the project. but you can always add it manually I guess.

Android Studio was complaining that org.apache.http classes like

org.apache.http.NameValuePair
org.apache.http.client.utils.URLEncodedUtils

were missing.

So I added org.apache.http.legacy.jar which is in Android/Sdk/platforms/android-23/optional folder to to app/libs

I also added this line to my app.gradle file

compile files('libs/org.apache.http.legacy.jar')

But if you're using more libraries, you can use this way

compile fileTree(dir: 'libs', include: ['*.jar'])

This resolved all my errors that were caused because google removed support of Apache HTTP client.

useLibrary 'org.apache.http.legacy' did not work for me until I upgraded the Gradle tools version in my main build.gradle file of my Android Studio project, as follows:

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

Legacy Apache library located in

[ANDROID_SDK]\platforms\android-23\optional\org.apache.http.legacy.jar

So you can copy it inside you project libs or just use

compile files("${android.getSdkDirectory().getAbsolutePath()}" + File.separator + "platforms" + File.separator + "android-23" + File.separator + "optional" + File.separator + "org.apache.http.legacy.jar")

in your /app/build.gradle

The answer above just helps the debug builds to run, and release builds that are utilizing gradle.

Insert this inside the application tag on the manifest file, on all project instances that uses the legacy apache classes:

<uses-library android:name="org.apache.http.legacy" android:required="false" />

This helps for those who are still using Eclipse and ant scripts during compile.

I know this is silly reason but at list try it...

I experienced this problem recently, and it is caused by the path length restriction I think it´s 256 characters maximum.

Relocate your Project and the build will succeed.Hope this work for you.

A simple way to solve this issue is C:\Users\username\AppData\Local\Android\sdk\platforms. Here delete your android-23 and from SDK manager update your API 23 again. It will solve your issue.

First you have to check that in your libs folder

Make sure in Libs Folder Check that apache library

Then add into your gradle file like this


android {
compileSdkVersion 23
buildToolsVersion '23.0.2'


defaultConfig {
applicationId "info.tranetech.laundry"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
android {
useLibrary 'org.apache.http.legacy'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1
compile 'com.android.support:design:23.0.1
testCompile 'junit:junit:4.12'
compile files('libs/android-async-http-1.4.4.jar')
compile 'com.google.android.gms:play-services:8.4.0'
}

This Is my gradle file

Also check external library

Enable this in sdk/platforms/android-23/optional/optional.json

[
{
"name": "org.apache.http.legacy",
"jar": "org.apache.http.legacy.jar",
"manifest": false
}
]

Remove

useLibrary 'org.apache.http.legacy'

from the build.gradle and I also added this line to my app.gradle file

compile files('libs/org.apache.http.legacy.jar')

But if you're using more libraries, you can use this way

compile fileTree(dir: 'libs', include: ['*.jar'])

CoPLaS answer fixed my problems.

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

To continue using Apache HTTP classes for API 23+:

First of all, be sure to add the gradle dependencie into the build.gradle f

buildscript {


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

Then add the reference inside build.gradle of your project:

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

After many frustrating hours, the following worked:

1. Locate the apache jar. It should reside somewhere like:

C:\Users\<yourname>\AppData\Local\Android\sdk\platforms\android-23\optional

2. Copy org.apache.http.legacy.jar to your libs folder.

Either right click on libs -> paste , or use your file explorer to navigate to the libs folder of your project and paste.

If you don't have a libs folder, as I did, make a new project and import all relevant files into their respective places.

3. Click ok see this

4. Most important step: Right click on the apache folder and select Add As Library. see this

Hope this helps someone get on with their life.