美文网首页
okhttp遇到的一点问题汇集

okhttp遇到的一点问题汇集

作者: 牵手生活 | 来源:发表于2020-05-08 15:14 被阅读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.ConscryptPlatformplatformTrustManager2>

    参考AndroidStudio解决依赖包冲突

    同步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();
    
            }
    
        }
    
    问题3:at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException
    image.png
    问题4 :java.net.SocketTimeoutException: timeout at okio.Okio$4.newTimeoutException(Okio.java:232)

    okhttp3之java.net.SocketTimeoutException: timeout 异常处理

    相关文章

      网友评论

          本文标题:okhttp遇到的一点问题汇集

          本文链接:https://www.haomeiwen.com/subject/yeyfnhtx.html