明文HTTP流量不允许

Nij*_*ali 1 xml android kotlin

我正在一个项目中工作,并且被困在登录/注册页面的实施中。当我尝试执行代码时,它给了我错误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()
Run Code Online (Sandbox Code Playgroud)

Sha*_*med 6

To do this in Android 9 Pie you will have to set a networkSecurityConfig in your Manifest application tag like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
     <application android:networkSecurityConfig="@xml/network_security_config">
     </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

Then create a xml file named network_security_config just like the way you have named it in the Manifest and the content of your file should be like thisto enable all requests without encryptions:

<?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>
Run Code Online (Sandbox Code Playgroud)

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


小智 6

将此添加到您的清单中

android:usesCleartextTraffic="true"
Run Code Online (Sandbox Code Playgroud)

像这样..

<application
    android:icon="@mipmap/ic_launcher"
    android:usesCleartextTraffic="true"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
...
 </application>
Run Code Online (Sandbox Code Playgroud)