OkHttp3超时设置和超时异常捕获

19 篇文章 0 订阅

OkHttp3超时设置

已经不能在 OkHttp3中使用的方法

为了容错和更好的用户体验,必须为OkHttp设置超时。

上网找了半天,只找到下面的代码。注意它们不能在 OkHttp3中使用

public ConfigureTimeouts() throws Exception {
  client = new OkHttpClient();
  client.setConnectTimeout(10, TimeUnit.SECONDS);
  client.setWriteTimeout(10, TimeUnit.SECONDS);
  client.setReadTimeout(30, TimeUnit.SECONDS);
}


以上代码中的方法setConnectTimeout,在OkHttp3中根本就不存在。


OkHttp3中设置超时的方法

上官网查资料,找到了新的方法

见网址: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超时

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

    public void GetServersList(IServersListEvent serversListEvent) {
        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);
            }
        });
    }


  • 18
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值