解决:qemu_pipe_open_ns:62: Could not connect to the ‘pipe:qemud:network‘ service: Invalid argument 202

Android studio版本: 3.5.2
API 30

问题

在使用okhttp+gson请求网络数据并解析的时候,请求解析失败:部分报错信息如下:
qemu_pipe_open_ns: Could not connect to the ‘pipe:qemud:network’ service: Invalid argument

RemoteConnection failed to initialize: RemoteConnection failed to open pipe

无法打开QEMU管道’qemud:network’:无效的参数(Failed to open QEMU pipe ‘qemud:network’: Invalid argument)

报错原因:
因为Android Emulator在7.0版本及以上提高了对不安全请求的限制级别,当我们的应用尝试向不安全的远程API(http)发送/接收请求/响应时,无法通过安全请求,就会发生这种情况。

解决方法:

方法一: 在Android清单文件application标记中配置降低安全级别解决
android:usesCleartextTraffic="true"

上面这个方法可以解决,但是缺点就是它倾向于对数据完整性构成威胁.所以我们更多选择下面这种方法来解决

方法二: 配置安全权限
  1. 右键res-> New-> Android Resource Directory。创建一个名为xml的资源目录
  2. 右键上面新建的xml文件夹:xml-> New-> Android Resource File
    命名为:network_security_config并加入如下代码:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your_site_domain.com</domain>
    </domain-config>
</network-security-config>
  1. 到你 AndroidManifest清单文件的应用程序application标记引用该文件
android:networkSecurityConfig="@xml/network_security_config" 

在这里插入图片描述
参考: https://www.it1352.com/2078606.html

猜你喜欢

转载自blog.csdn.net/weixin_43853746/article/details/111411752