在SDK27以上版本中,如何发送http请求

SDK27以上以上的,有网络安全限制,请求的协议必须是https.如果仍需要发起http请求的,需要做一下配置:
1 .res下新建一个xml的包,然后在新建一个network_security_config.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <!--禁用掉明文流量请求的检查-->
    <base-config cleartextTrafficPermitted="true" />
        <trust-anchors>
            <certificates src="system" overridePins="true" />
            <certificates src="user" overridePins="true" />
        </trust-anchors>
    </base-config>
</network-security-config>

2 .在AndroidManifest.xml文件中添加网络权限和引入文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ttit.myapp">
    ..........
    <uses-permission android:name="android.permission.INTERNET" />//添加网络权限
    <application
    	..........
        android:networkSecurityConfig="@xml/network_security_config"//引入文件
        .....
    </application>

</manifest>

猜你喜欢

转载自blog.csdn.net/weixin_55806809/article/details/121354815
今日推荐