简体   繁体   English

如何在Android Marshmallow上使用旧版Apache HTTP客户端?

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

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. 在Android Marshmallow上,Google已完全删除了对Apache HTTP客户端的支持(链接此处 ),因为与其他替代品相比,它的性能不佳。

This might also be the cause for so many apps crashing on Android Marshmallow. 这也可能是导致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: 通过将以下行添加到gradle文件中,Google允许您仍然使用此API,而不是内置API:

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

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. 我知道不建议您使用此解决方案,但是我们必须至少暂时准备好该应用程序,直到我们100%应对Android Marshmallow应当更改的所有事情,并且我们不希望出现意外情况崩溃的形式。

Here's a screenshot: 这是屏幕截图:

在此处输入图片说明

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 https://code.google.com/p/android/issues/detail?id=181474

Android Studio was complaining that org.apache.http classes like Android Studio抱怨org.apache.http类像

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 所以我在Android/Sdk/platforms/android-23/optional文件夹中添加了org.apache.http.legacy.jarapp/libs

I also added this line to my app.gradle file 我也将此行添加到我的app.gradle文件中

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. 这解决了我所有由于Google删除了对Apache HTTP客户端的支持而导致的错误。

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: useLibrary的“ org.apache.http.legacy”对我不起作用,直到我在Android Studio项目的主build.gradle文件中升级了Gradle工具版本,如下所示:

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

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 / Users /“您的名字” /开发/android-sdk-macosx/platforms/android-MNC/android.jar / Users /“您的名字” /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. 但我想您总是可以手动添加它。

After many frustrating hours, the following worked: 经过许多令人沮丧的小时,以下工作:

1. Locate the apache jar. 1.找到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. 2.将 org.apache.http.legacy.jar 复制 到您的libs文件夹。

Either right click on libs -> paste , or use your file explorer to navigate to the libs folder of your project and paste. 右键单击libs-> paste,或使用文件资源管理器导航到项目的libs文件夹并粘贴。

If you don't have a libs folder, as I did, make a new project and import all relevant files into their respective places. 如果没有像我一样的libs文件夹,请创建一个新项目并将所有相关文件导入各自的位置。

3. Click ok see this 3.单击ok 查看此

4. Most important step: Right click on the apache folder and select Add As Library . 4.最重要的步骤:右键单击apache文件夹,然后选择Add As Library see this 看到这个

Hope this helps someone get on with their life. 希望这可以帮助某人过上幸福的生活。

Legacy Apache library located in 旧版Apache库位于

[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 在您的/app/build.gradle中

The answer above just helps the debug builds to run, and release builds that are utilizing gradle. 上面的答案仅有助于调试版本的运行,以及发布利用gradle的版本。

Insert this inside the application tag on the manifest file, on all project instances that uses the legacy apache classes: 在使用旧版apache类的所有项目实例上,将其插入清单文件中的application标记内:

<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. 这对于那些在编译期间仍在使用Eclipse和ant脚本的人有帮助。

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. 我最近遇到了这个问题,这是由路径长度限制引起的,我认为最大长度为256个字符。

Relocate your Project and the build will succeed.Hope this work for you. 迁移您的项目,构建将成功。希望为您完成这项工作。

First you have to check that in your libs folder 首先,您必须在libs文件夹中进行检查

确保在Libs文件夹中检查该apache库

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

这是我的gradle文件

同时检查外部库

Enable this in sdk/platforms/android-23/optional/optional.json 在sdk / platforms / android-23 / optional / optional.json中启用此功能

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

A simple way to solve this issue is C:\\Users\\username\\AppData\\Local\\Android\\sdk\\platforms. 解决此问题的简单方法是C:\\ Users \\用户名\\ AppData \\ Local \\ Android \\ sdk \\ platforms。 Here delete your android-23 and from SDK manager update your API 23 again. 在这里删除您的android-23,然后从SDK管理器中再次更新您的API 23。 It will solve your issue. 它将解决您的问题。

Remove 去掉

useLibrary 'org.apache.http.legacy' 

from the build.gradle and I also added this line to my app.gradle file 从build.gradle中,我还将这一行添加到我的app.gradle文件中

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. CoPLaS回答解决了我的问题。

How to use the legacy Apache HTTP client on Android Marshmallow? 如何在Android Marshmallow上使用旧版Apache HTTP客户端?

To continue using Apache HTTP classes for API 23+: 要继续为API 23+使用Apache HTTP类:

First of all, be sure to add the gradle dependencie into the build.gradle f 首先,确保将gradle依赖项添加到build.gradle f中。

buildscript {

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

Then add the reference inside build.gradle of your project: 然后在项目的build.gradle中添加引用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM