Android应用无法发送HTTP请求。

huangapple go评论93阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;network-security-config&gt;
    &lt;domain-config cleartextTrafficPermitted=&quot;true&quot;&gt;
        &lt;domain includeSubdomains=&quot;true&quot;&gt;YOUR_IP&lt;/domain&gt;
    &lt;/domain-config&gt;
&lt;/network-security-config&gt;

and then add android:networkSecurityConfig=&quot;@xml/network_security_config&quot; 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 -
如果在 中有 android:targetSandboxVersion,则将其减小为 1

在 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 &quot;https://&quot; instead of &quot;http://&quot;

Option 2 -

Create file res/xml/network_security_config.xml -

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;network-security-config&gt;
    &lt;domain-config cleartextTrafficPermitted=&quot;true&quot;&gt;
        &lt;domain includeSubdomains=&quot;true&quot;&gt;api.example.com(to be adjusted)&lt;/domain&gt;
    &lt;/domain-config&gt;
&lt;/network-security-config&gt;
AndroidManifest.xml -

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest ...&gt;
    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
    &lt;application
        ...
        android:networkSecurityConfig=&quot;@xml/network_security_config&quot;
        ...&gt;
        ...
    &lt;/application&gt;
&lt;/manifest&gt;
Option 3 -

android:usesCleartextTraffic Doc

AndroidManifest.xml -

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest ...&gt;
    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
    &lt;application
        ...
        android:usesCleartextTraffic=&quot;true&quot;
        ...&gt;
        ...
    &lt;/application&gt;
&lt;/manifest&gt;

 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 &lt;manifest&gt; then reduce it to 1

AndroidManifest.xml -

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest android:targetSandboxVersion=&quot;1&quot;&gt;
    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
    ...
&lt;/manifest&gt;

huangapple
  • 本文由 发表于 2020年10月5日 16:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64204737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定