明文的HTTP流量不被允许。

9
我在一个项目中工作,卡在了登录/注册页面的实现上。当我尝试实现代码时,它会给我一个错误:Cleartext HTTP traffic to 192.168.1.130 not permitted。 我检查了 ipconfig,那是我的IPv4,所以我添加了192.168.1.130,但我还检查了127.0.0.1,但那个也不起作用。
我尝试了实现<domain includeSubdomains="true">192.168.1.130</domain>,但那个也不起作用(实现android:usesCleartextTraffic="true"也不行)。
我使用XAMPP作为后端,如果我运行PHP代码,一切都很顺利,所以没有问题。问题在于Android Studio(我使用Kotlin)。
对于模拟器,我使用Genymotion模拟器(它使用VirtualBox)。
这里是获取url的按钮代码。我已经检查了一切,但仍然无法解决问题。
login.setOnClickListener {
            var url = "http://192.168.1.130/php/login.php?mobile=" + login_user.text.toString() +
                    "&password=" + login_password.text.toString()
3个回答

31

在Android 9 Pie中,您需要在清单(Manifest)应用标签中设置networkSecurityConfig,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
     <application android:networkSecurityConfig="@xml/network_security_config">
     </application>
</manifest>

那么按照您在清单文件中命名的方式创建一个名为network_security_config的xml文件,文件内容应该如下所示以允许所有未加密请求:

然后创建一个名为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>

如果这不起作用,请从安全域名(HTTPS)发出您的请求。


谢谢,但是你看了描述吗?我已经说过那个方法不起作用了。 - Nijat Mursali
请从安全域名(HTTPS)发出您的请求。 - Shafi Muhammed
我再次检查了您的答案,它完美地工作了,所以我将把它设为被接受的答案。 - Nijat Mursali

9
将以下内容加入您的清单文件中:
android:usesCleartextTraffic="true"

like this..

<application
    android:icon="@mipmap/ic_launcher"
    android:usesCleartextTraffic="true"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
...
 </application>

1
属性 usesCleartextTraffic 仅在 API 级别 23 及以上版本中使用。 - zeleven
@zeleven 请查看这个链接 [https://dev59.com/elYM5IYBdhLWcg3wpRba#50942262] - Rahul
我已经检查过了,但不幸的是它没有解决问题。请检查上面接受的答案。 - Nijat Mursali
错误:未找到属性android:usesCleartextTraffic。 - Androidcoder
@Androidcoder 在 Manifest 文件中是否需要添加 android:usesCleartextTraffic="true" 这一行? - Rahul
com.android.volley.NoConnectionError: java.net.ConnectException: 无法连接到/127.0.0.1:8000。 - Fernando Torres

4

我最近在使用安卓9.0系统时也遇到了同样的问题。

我在Manifest文件中进行了如下添加:

android:usesCleartextTraffic="true"
tools:ignore="UnusedAttribute"

第二行提到的是忽略SDK版本低于23的警告。


谢谢您的答复。我非常感激。这个对我也有用。 - Nijat Mursali
我发现在 Android SDK 版本低于 23 的情况下,使用 tools:ignore="UnusedAttribute" 可以有效地抑制警告。 - ilatyphi95

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接