安卓网络请求
作者:互联网
遇到的几个坑
1 需要申请权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2 不能在主线程发请求
解决:创建子线程或者设置代码
code:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
或者创建子线程:
new Thread(new Runnable(){
@Override
public void run() {
sendmsg(data);
}
}).start();
参考:
https://blog.csdn.net/qq_29477223/article/details/81027716
标签:请求,安卓,StrictMode,网络,ThreadPolicy,线程,policy,new 来源: https://blog.csdn.net/qq_38376348/article/details/112429770