okhttp遇到的一点问题汇集
2020-05-08 本文已影响0人
牵手生活
参考资料
OkHttp-官方资料
Okhttp源码分析以及Google Gson解析json数据实例-response.body.string()不能调用两次
okhttp3同步请求使用总结
AndroidStudio解决依赖包冲突
Android OkHttp常用详解
问题1:Rejecting re-init on previously-failed class java.lang.Class<okhttp3.internal.platform.ConscryptPlatform2>
同步post 出现at okhttp3.internal.Util.readBomAsCharset(Util.kt:258) at okhttp3.ResponseBody.string(ResponseBody.kt:187)
解决办法:运用Okhttp框架遇到的坑:https://blog.csdn.net/weixin_45200954/article/details/105513738
android.os.NetworkOnMainThreadException
解决办法创建一个进程
try {
new Thread(new Runnable() { // android.os.NetworkOnMainThreadException
@Override
public void run() {
HttpUtil.doJsonPostByOkhttp(cloundConfig.getSynctoserver(), JSON.toJSONString(data));
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
doJsonPostByOkhttp代码:
/**
*
* @param urlPath
* @param dataJson //672正常
* @return https://square.github.io/okhttp/
* https://www.iteye.com/blog/xuanzhui-2284601
*/
public static String doJsonPostByOkhttp(String urlPath, String dataJson){
MyLog.d("okhttp3 Sync","请求url===="+urlPath+"\n请求参数"+dataJson);
MediaType JSON = MediaType.get("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON,dataJson);
Request request = new Request.Builder()
.url(urlPath)
.post(body)
.build();
//proceedRequest(client, request, response);
try{
Response response = client.newCall(request).execute(); // android.os.NetworkOnMainThreadException
//运用Okhttp框架遇到的坑:https://blog.csdn.net/weixin_45200954/article/details/105513738 System.err: at okhttp3.internal.Util.readBomAsCharset(Util.kt:258) at okhttp3.ResponseBody.string(ResponseBody.kt:187)
Log.i("okhttp3","response.code()"+response.code());
String str_boty = response.body().string();
Log.i("okhttp3",str_boty);
Log.i("okhttp3",response.message());
return str_boty;
}catch (IOException e) {
MyLog.i("okhttp3",e.getMessage());
return "okhttp3异常"+e.getMessage();
}
}