org.apache.http.legacy在API 28上不起作用

Joh*_*ans 3 apache android http

首先,我使用org.apache.http函数已经有一段时间了,当我尝试在API 28上启动我的应用程序时出现了错误。它在API 26和API 23上运行,但是API 28突然出现了问题。 Google进行了一些更改?

Git*_*esh 9

要在Android 9.0 Pie中完美运行org.apache.http.legacy,请创建一个xml文件 res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
      <base-config cleartextTrafficPermitted="true">
       <trust-anchors>
        <certificates src="system" />
       </trust-anchors>
      </base-config>
    </network-security-config>
Run Code Online (Sandbox Code Playgroud)

并在您的AndroidManifest.xml中添加2个标签

android:networkSecurityConfig =“ @ xml / network_security_config” android:name =“ org.apache.http.legacy”

<?xml version="1.0" encoding="utf-8"?>
 <manifest......>
  <application android:networkSecurityConfig="@xml/network_security_config">
   <activity..../> 
   ......
   ......
 <uses-library
        android:name="org.apache.http.legacy"
        android:required="false"/>
</application>
Run Code Online (Sandbox Code Playgroud)

还添加useLibrary 'org.apache.http.legacy'您的应用程序构建gradle

android {
compileSdkVersion 28
defaultConfig {
    applicationId "your application id"
    minSdkVersion 15
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    useLibrary 'org.apache.http.legacy'
}
Run Code Online (Sandbox Code Playgroud)

  • 唯一为我解决问题的事情。花了将近一天的时间来解决这个问题。谢谢你! (3认同)

Asa*_*sad 6

步骤1:在其中
创建一个xml文件,res/xml/network_security_config.xml然后在其中复制以下代码,

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your.website.domain</domain> <!-- the domain you are using for api network call -->
    </domain-config>
</network-security-config>
Run Code Online (Sandbox Code Playgroud)

第2步:
现在你Manifest.xmlandroid:networkSecurityConfig="@xml/network_security_config"<application>标记。做完了!


Ram*_*lix 6

你只需要做两件事:

1-将 AndroidManifest.xml 下一行放入<application>

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

2-将调用的 URL 更改为 HTTPS 而不是 HTTP

完成此操作后,错误消失,我的应用程序再次运行。


sol*_*ver 1

Apache HTTP 客户端弃用

在 Android 6.0 中,我们删除了对 Apache HTTP 客户端的支持。从 Android 9 开始,该库已从 bootclasspath 中删除,并且默认情况下不可用于应用程序。