gpt4 book ai didi

java - 如何修复 'Cleartext HTTP traffic to x not permitted'

转载 作者:行者123 更新时间:2023-12-04 12:14:22 27 4
gpt4 key购买 nike

我正在尝试向 http 服务器发出 post 请求,但是当我尝试获取输入流时,我收到错误 java.io.IOException: Cleartext HTTP traffic to x not permitted
我已经试过把 android:usesCleartextTraffic="true"在我的 list 中,以及进行网络安全配置并将 android:targetSandboxVersion 设置为 1

app/src/main/res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">Server adress</domain>
</domain-config>
</network-security-config>

应用程序/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.packagename"
android:targetSandboxVersion="1">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Logcat 输出
D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
W/System.err: java.io.IOException: Cleartext HTTP traffic to x not permitted
W/System.err: at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:124)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:462)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248)

任何帮助,将不胜感激

最佳答案

尝试仅使用

android:usesCleartextTraffic="true"

删除这个 2
android:targetSandboxVersion="1"
android:networkSecurityConfig="@xml/network_security_config"

我的只是使用 android:usesCleartextTraffic="true"在任何 API 中工作

关于java - 如何修复 'Cleartext HTTP traffic to x not permitted',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58022495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com