如何解决网络安全政策不允许 Android 9.0 CLEARTEXT 与“ipaddress”通信
当我运行我的 android 项目时,将目标 url https 更改为 http,出现错误“网络安全策略不允许与 mylocalipaddress 的 CLEARTEXT 通信”。
因此,我在 mainfest 标签中将 AndroidManifest.xml 编辑为 android:targetSandBoxVersion="1"
,并将 network_security_config.xml 编辑为
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">mylocalipaddress</domain>
<domain includeSubdomains="true">myipdomain:3000</domain>
</domain-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
但它们不起作用,我也遇到了同样的错误。 我不能确定的是 Q> 网络安全配置标签下的域配置标签和基本配置标签是否可以同时存在?如果可以,我该怎么办?
我的 AndroidManifest.xml 是
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.test.test_iaq"
xmlns:tools="http://schemas.android.com/tools"
android:targetSandBoxVersion="1">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<Meta-data android:name="PW_broADCAST_PUSH" android:value="true"/>
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher"
android:allowBackup="true"
android:theme="@style/AppTheme"
android:largeHeap="true"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning"
android:networkSecurityConfig="@xml/network_security_config"
tools:replace="android:allowBackup">
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!--external link-->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.broWSABLE" />
<data android:scheme="test"
android:host="jwt"/>
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>
<activity android:name="com.kakao.auth.authorization.authcode.KakaoWebViewActivity"
android:launchMode="singletop"
android:exported="false"
android:windowSoftInputMode="adjustResize">
</activity>
</application>
</manifest>
此外,当 POST 请求的端点为 {ipaddress}:3000 大小写时,是否会出现此错误?我的意思是 url 不是域名,http://ipaddress:port 大小写。
解决方法
您应该根据 documentation 为 AndroidManifest.xml 设置 android:networkSecurityConfig
属性。否则,Android 将看不到您的 network_security_config.xml 文件:
android:networkSecurityConfig="@xml/network_security_config"
请注意,如果您设置了 android:networkSecurityConfig,android:usesCleartextTraffic 属性将被忽略。
是的,base-config 和 domain-config can be 同时存在。
此外,域标签中不允许使用端口号。仅使用域名或 IP 地址:
<domain includeSubdomains="true">example.com</domain>
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">127.0.0.1</domain>
,
第 1 步:
<?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>
第 2 步:
加入AndroidMainfest
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
,
Android 现在禁止明文网络通信。它假定所有内容都已加密。
要允许未加密的内容,您需要将此行添加到 AndroidManifest.xml:
android:usesCleartextTraffic="true"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。