Android 应用程序无法发送 http 请求
Android application unable to send http requests
我有一个 android 应用程序,它使用 Retrofit 发出 http 请求 (POST) 并进一步评估传入数据。
我的应用程序在我的测试 phone (Samsung J5 P) 中运行得非常好,它可以正常连接和发送请求。但是在尝试在两个不同的 phone 上同时操作该应用程序时,另一个 phone 上的应用程序以某种方式无法发送请求(连接失败时改造调用 onFailure 方法)。我已经启用了所有权限,但问题似乎发生了。
此外,我在桌面 (localhost) 上托管了服务器,并确保我的两台设备都连接到同一网络。
是什么导致了这个问题?谢谢
在 res/xml 目录中创建网络配置文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">YOUR_IP</domain>
</domain-config>
</network-security-config>
然后在清单的应用程序标签中添加 android:networkSecurityConfig="@xml/network_security_config"
从 Android 9(API 级别 28)开始,默认禁用明文支持。
也看看 - https://koz.io/android-m-and-the-war-on-cleartext-traffic/
Codelab 说明 - https://codelabs.developers.google.com/codelabs/android-network-security-config/index.html
Option 1 -
First try hitting the URL with "https://" instead of "http://"
Option 2 -
Create file res/xml/network_security_config.xml -
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
</domain-config>
</network-security-config>
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>
Option 3 -
android:usesCleartextTraffic Doc
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
android:targetSandboxVersion can be a problem too -
According to Manifest Docs -
android:targetSandboxVersion
The target sandbox for this app to use. The higher the sandbox version number, the higher the level of security. Its default value is 1; you can also set it to 2. Setting this attribute to 2 switches the app to a different SELinux sandbox. The following restrictions apply to a level 2 sandbox:
The default value of usesCleartextTraffic in the Network Security Config is false.
Uid sharing is not permitted.
So Option 4 -
If you have android:targetSandboxVersion in <manifest> then reduce it to 1
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest android:targetSandboxVersion="1">
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
我有一个 android 应用程序,它使用 Retrofit 发出 http 请求 (POST) 并进一步评估传入数据。 我的应用程序在我的测试 phone (Samsung J5 P) 中运行得非常好,它可以正常连接和发送请求。但是在尝试在两个不同的 phone 上同时操作该应用程序时,另一个 phone 上的应用程序以某种方式无法发送请求(连接失败时改造调用 onFailure 方法)。我已经启用了所有权限,但问题似乎发生了。
此外,我在桌面 (localhost) 上托管了服务器,并确保我的两台设备都连接到同一网络。
是什么导致了这个问题?谢谢
在 res/xml 目录中创建网络配置文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">YOUR_IP</domain>
</domain-config>
</network-security-config>
然后在清单的应用程序标签中添加 android:networkSecurityConfig="@xml/network_security_config"
从 Android 9(API 级别 28)开始,默认禁用明文支持。
也看看 - https://koz.io/android-m-and-the-war-on-cleartext-traffic/
Codelab 说明 - https://codelabs.developers.google.com/codelabs/android-network-security-config/index.html
Option 1 -
First try hitting the URL with "https://" instead of "http://"
Option 2 -
Create file res/xml/network_security_config.xml -
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
</domain-config>
</network-security-config>
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>
Option 3 -
android:usesCleartextTraffic Doc
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
android:targetSandboxVersion can be a problem too -
According to Manifest Docs -
android:targetSandboxVersion
The target sandbox for this app to use. The higher the sandbox version number, the higher the level of security. Its default value is 1; you can also set it to 2. Setting this attribute to 2 switches the app to a different SELinux sandbox. The following restrictions apply to a level 2 sandbox:
The default value of usesCleartextTraffic in the Network Security Config is false.
Uid sharing is not permitted.
So Option 4 -
If you have android:targetSandboxVersion in <manifest> then reduce it to 1
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest android:targetSandboxVersion="1">
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>