不允许明文 HTTP 流量到我的本地主机



我正在使用 Volley 向我的本地主机上的地址发出 GET 请求,但它失败并出现错误:

Cleartext HTTP traffic to 192.168.1.45 not permitted

我遵循了这里的指南:Android 8:不允许明文HTTP流量 并做了以下工作:

已创建网络安全 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">http://192.168.1.45/companyweb/greetings</domain>
</domain-config>
</network-security-config>

将其添加到我的清单中,还允许明文流量:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omerfaran.myudemyapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"

我仍然遇到同样的错误。从"http"更改为"https"会给出此错误:

socket failed: EPERM (Operation not permitted)

我在主活动中的代码:

val url = "http://192.168.1.45/companyweb/greetings"
val rq = Volley.newRequestQueue(this)
val sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
fragmentText.text = response
Log.d("TAG", "success")
}, Response.ErrorListener { error -> Log.d("TAG", "fail" + error.toString()) })
rq.add(sr)

接下来我能做什么?

您应该只包含 IP 地址,即:

<domain includeSubdomains="true">192.168.1.45</domain>

我在最近的一个项目中遇到了类似的问题,请尝试添加:

android:usesCleartextTraffic="true">

到您的 AndroidManifext.xml 应用程序标记下的文件。

我在这里找到了答案: java.net.SocketException: socket failed: EPERM (不允许操作(

显然,我所要做的就是重新启动模拟器(或卸载应用程序(

最新更新