遇到的几个坑
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