无法连接到Android 10中的HTTP URL [英] Unable connect to a HTTP URL in Android 10

查看:228
本文介绍了无法连接到Android 10中的HTTP URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 http://something API进行登录.但是在Android 10中无法获得API的成功.在该版本的其余版本中,API可以正常工作.首先,我得到了 SocketTimeoutException .然后,我尝试了以下解决方案.

I am using a http://something API for the login process. But could't get a hit on API in Android 10. For rest of the version, the API is working fine. First of all, I was getting SocketTimeoutException. Then I tried following solutions.

1)在清单中的< application> 中添加了以下属性.

1) Added below attribute to <application> in Manifest.

android:usesCleartextTraffic="true"

结果:仍然出现 SocketTimeoutException .

2)然后我添加了 networkSecurityConfig :

<network-security-config>
<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">project.dev.company:6001/</domain>
    <trust-anchors>
        <certificates src="system"/>
    </trust-anchors>
</domain-config>
</network-security-config>

结果: UnknownServiceException:网络安全策略不允许与project.dev.company:6001/的CLEARTEXT通信.

3)还尝试允许在< base-config> 中使用CLEARTEXT.仍然会收到 SocketTimeoutException .

3) Also tried permitting CLEARTEXT in <base-config>. Still getting SocketTimeoutException.

如何允许我的应用从Android 10访问HTTP连接?我正在使用Retrofit2进行网络通话.

How can I permit my app to access a HTTP connection from Android 10? I am using Retrofit2 for network calling.

推荐答案

在您的xml文件中使用此代码

Use this codes in your xml file

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
   <domain-config cleartextTrafficPermitted="true">
      <domain includeSubdomains="true">project.dev.company</domain>
   </domain-config>
</network-security-config>

并在清单中使用以下代码:

and use this codes in manifest:

android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_configuration"

不需要任何"/"和域中的端口,只需像我一样使用

dont need any "/" and port in domain,just use like me

您可以使用api主机的IP,如下所示:

you can use IP of your api host,like this:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
      <domain includeSubdomains="true">192.168.1.1</domain>
   </domain-config>
</network-security-config>

这篇关于无法连接到Android 10中的HTTP URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆