Joh*_*ans 3 apache android http
首先,我使用org.apache.http函数已经有一段时间了,当我尝试在API 28上启动我的应用程序时出现了错误。它在API 26和API 23上运行,但是API 28突然出现了问题。 Google进行了一些更改?
要在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)
步骤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.xml加android:networkSecurityConfig="@xml/network_security_config"下<application>标记。做完了!
你只需要做两件事:
1-将 AndroidManifest.xml 下一行放入<application>
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
2-将调用的 URL 更改为 HTTPS 而不是 HTTP
完成此操作后,错误消失,我的应用程序再次运行。
在 Android 6.0 中,我们删除了对 Apache HTTP 客户端的支持。从 Android 9 开始,该库已从 bootclasspath 中删除,并且默认情况下不可用于应用程序。
| 归档时间: |
|
| 查看次数: |
5786 次 |
| 最近记录: |