How to use the legacy Apache HTTP client on Androi

2019-01-01 15:15发布

Background

On Android Marshmallow, Google has completely removed the support of Apache HTTP client (link here) because it doesn't have good performance compared to the alternatives.

This might also be the cause for so many apps crashing on Android Marshmallow.

The problem

Google allows you to still use this API, just not as a built in one, by adding this line to the gradle file:

useLibrary 'org.apache.http.legacy'

So, this is what I did:

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

And:

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

When I tried it, it compiled fine (no errors being shown, and I could run the proof-of-concept app, as it doesn't have any special code), but when I tried using some of the classes that I know that are part of the old API (like "HttpClient" class), I see that it doesn't allow me to do so.

I know it's not recommended to use this solution, but we must have the app ready to work there at least temporarily, till we work 100% on all of the things that should change for Android Marshmallow, and we don't want surprises in the form of crashes.

Here's a screenshot:

enter image description here

The question

Why does it occur? Did I use it correctly?


EDIT: reported about this issue here:

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

12条回答
深知你不懂我心
2楼-- · 2019-01-01 15:46

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.

查看更多
泛滥B
3楼-- · 2019-01-01 15:52

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

[
  {
    "name": "org.apache.http.legacy",
    "jar": "org.apache.http.legacy.jar",
    "manifest": false
  }
]
查看更多
看淡一切
4楼-- · 2019-01-01 15:54

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.

查看更多
妖精总统
5楼-- · 2019-01-01 15:55

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

查看更多
忆尘夕之涩
6楼-- · 2019-01-01 15:58

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.

查看更多
旧人旧事旧时光
7楼-- · 2019-01-01 15:59

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.

查看更多
登录 后发表回答