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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31653002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:22:37  来源:igfitidea点击:

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

javaandroidandroid-gradle-pluginandroid-6.0-marshmallow

提问by android developer

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:

这是一个屏幕截图:

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

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

回答by WenChao

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/"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.

好了,jar 就在那里。出于某种原因,它没有被添加到项目中。但我猜你总是可以手动添加它。

回答by CoPLaS

Android Studio was complaining that org.apache.httpclasses 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.jarwhich is in Android/Sdk/platforms/android-23/optionalfolder to to app/libs

所以我添加org.apache.http.legacy.jarAndroid/Sdk/platforms/android-23/optional文件夹中的哪个 到app/libs

I also added this line to my app.gradlefile

我还将这一行添加到我的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 客户端的支持而导致的所有错误。

回答by Jamie Hall

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:

在我升级 Android Studio 项目的主 bu​​ild.gradle 文件中的 Gradle 工具版本之前,useLibrary 'org.apache.http.legacy' 对我不起作用,如下所示:

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

回答by AndreyICE

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 libsor 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

回答by Pier Betos

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 类的所有项目实例上:

<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 脚本的人有帮助。

回答by Arpit Patel

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.

重新定位您的项目,构建将成功。希望这对您有用。

回答by impathuri

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.

解决此问题的一个简单方法是 C:\Users\username\AppData\Local\Android\sdk\platforms。在这里删除您的 android-23 并从 SDK 管理器中再次更新您的 API 23。它将解决您的问题。

回答by Arpit Patel

First you have to check that in your libs folder

首先你必须在你的 libs 文件夹中检查

Make sure in Libs Folder Check that apache library

确保在 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'
    }

This Is my gradle file

这是我的gradle文件

Also check external library

还要检查外部库

回答by arun-r

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

回答by Roger Belk

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