美文网首页Android 进阶之路
OkHttp3中设置超时的方法

OkHttp3中设置超时的方法

作者: 雪纳瑞的哈士奇 | 来源:发表于2018-12-03 17:20 被阅读38次

    见网址:http://square.github.io/okhttp/3.x/okhttp/

    Method Detail

    connectTimeout

    public OkHttpClient.Builder connectTimeout(long timeout,
    TimeUnit unit)

    Sets the default connect timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

    readTimeout

    public OkHttpClient.Builder readTimeout(long timeout,
    TimeUnit unit)

    Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

    writeTimeout

    public OkHttpClient.Builder writeTimeout(long timeout,
    TimeUnit unit)

    Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

    从上面可以看到设置超时移到了OkHttpClient.Builder中,所以最新的设置超时的代码如下:

       public WebApi(){
            client = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS)
                    .build();
        }
    

    捕获OkHttp3超时

    然后捕获异常,加以处理。

         this.serversListEvent = serversListEvent;
         serversLoadTimes = 0;
         Request request = new Request.Builder()
                 .url(serversListUrl)
                 .build();
         client.newCall(request).enqueue(new Callback() {
    
             @Override
             public void onFailure(Call call, IOException e) {
                 if(e.getCause().equals(SocketTimeoutException.class) && serversLoadTimes<maxLoadTimes)//如果超时并未超过指定次数,则重新连接
                 {
                     serversLoadTimes++;
                     client.newCall(call.request()).enqueue(this);
                 }else {
                     e.printStackTrace();
                     WebApi.this.serversListEvent.getServers(null);
                 }
             }
    
             @Override
             public void onResponse(Call call, Response response) throws IOException {
                 String html = new String(response.body().bytes(), "big5");
                 Matcher m = serversListPattern.matcher(html);
                 ServersList serverList = new ServersList();
                 while (m.find()){
                     serverList.add(new ServerInfo(m.group(1), m.group(2)));
                 }
    
                 Matcher mc1 = selectServerCodePattern.matcher(html);
                 Matcher mc2 = selectCityCodePattern.matcher(html);
                 if(mc1.find())
                     serverList.selectServerCode=mc1.group(1);
                 if(mc2.find())
                     serverList.selectCityCode=mc2.group(1);
    
                 WebApi.this.serversListEvent.getServers(serverList);
             }
         });
     }
    

    原文:https://blog.csdn.net/do168/article/details/51848895
    版权声明:本文为博主原创文章,转载请附上博文链接!

    相关文章

      网友评论

        本文标题:OkHttp3中设置超时的方法

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