我正在尝试向创建自己的wifi热点且仅允许http请求的设备发出http请求。使用android 8和更低的http请求成功,但是使用android 9则失败。 尽管使用Google Pixel 2似乎可以正常工作,但它使用的是Android 9。
我尝试在AndroidManifest.xml中启用android:usesCleartextTraffic =“ true”。
我按照此处How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?的解决方案中的建议设置networkSecurityConfig
AndroidManifestXml
<application
android:networkSecurityConfig="@xml/network_security_config"
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTask"
android:usesCleartextTraffic="true">
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>
答案 0 :(得分:0)
最后找到解决方案。因为我要连接的设备没有互联网连接。当我手动连接到设备时,我看到android 9向我显示了以下文本弹出窗口:wifi没有互联网,您确定要连接到它。
解决方案是使用forceWifi()函数,该函数强制android使用连接。