英文:
Android application unable to send http requests
问题
我有一个使用Retrofit进行HTTP请求(POST)并进一步评估传入数据的Android应用程序。
我的测试手机(三星J5 P)上的应用程序工作得非常好,它可以正常连接并发送请求。但是当我尝试在两部不同的手机上同时操作应用程序时,另一部手机上的应用程序在发送请求时会出现问题(当连接失败时,Retrofit调用onFailure方法)。我已经授予了所有权限,但问题似乎仍然发生。
另外,我已经将服务器托管在我的台式机上(本地主机),并确保我两台设备连接到同一网络。
是什么原因导致了这个问题?谢谢。
英文:
I have an android application that uses Retrofit to make http requests (POST) and further evaluate incoming data.
I have the app working absolutely fine in my test phone (Samsung J5 P), it connects and sends requests as it should. But on trying operating the app simultaneously on two different phones, the app on the other phone somehow fails to send requests (retrofit calls onFailure method when a connection fails). I have enabled all permissions, yet the problem seems to happen.
Also, I have hosted the server on my desktop (localhost), and I made sure both my devices are connected to same network.
What could be causing this problem? Thanks.
答案1
得分: 0
在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"`
英文:
Create a network config file in res/xml directory
<?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>
and then add android:networkSecurityConfig="@xml/network_security_config"
in your manifest's application tag
答案2
得分: 0
开始自 Android 9(API 级别 28),默认禁用明文支持。
此外,还可以查看 - https://koz.io/android-m-and-the-war-on-cleartext-traffic/
Codelabs 解释 - https://codelabs.developers.google.com/codelabs/android-network-security-config/index.html
选项 1 -
首先尝试使用 "https://" 而不是 "http://"
选项 2 -
创建文件 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(需要调整)</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>
选项 3 -
android:usesCleartextTraffic 文档
在 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 也可能是一个问题 -
根据清单文档 -
android:targetSandboxVersion
此应用程序要使用的目标沙箱。沙箱版本号越高,安全级别越高。其默认值为 1;还可以将其设置为 2。将此属性设置为 2 将会将应用切换到另一个 SELinux 沙箱。以下限制适用于级别 2 沙箱:
网络安全配置中 usesCleartextTraffic 的默认值为 false。
不允许 UID 共享。
因此,选项 4 -
如果在
在 AndroidManifest.xml 中 -
<?xml version="1.0" encoding="utf-8"?>
<manifest android:targetSandboxVersion="1">
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
英文:
Starting with Android 9 (API level 28), cleartext support is disabled by default.
Also have a look at - https://koz.io/android-m-and-the-war-on-cleartext-traffic/
Codelabs explanation - 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论