OkHTTP中的处理超时 [英] Handle timeout in OkHTTP

查看:621
本文介绍了OkHTTP中的处理超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在OkHTTP中捕获超时?是onFailure方法中的调用"吗?

How can i catch the timeout in OkHTTP? Is it "called" in the onFailure method?

我想处理我的互联网连接不良的情况.如果超时,我必须启用一些按钮以允许用户重试.

I would like to handle the situation in which i have bad internet connection. In case of timeout i have to enable some buttons to allow the user to try again.

// Get a handler that can be used to post to the main thread
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            hideDialog();
            e.printStackTrace();

            // handle timeouts here, enable buttons...
        }

推荐答案

您可以增加连接超时和请求响应.使用以下代码,

You can increase timeout for connection and response for your request. Use following code,

client.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout
client.setReadTimeout(15, TimeUnit.SECONDS);    // socket timeout

这篇关于OkHTTP中的处理超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆