我已经使用okhttp3来请求network.sometimes当我提出网络请求并且我已经设置
mOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
.readTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
.writeTimeout(CONNECTION_TIME_OUT, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.build();
CONNECTION_TIME_OUT是10.但是15秒后我无法得到答复。如果我还在等待,请求仍然没有响应。最像请求正在进行中。 因为当我关闭我的wifi时,requst可以响应,结果是超时异常。我这是不对的? 然后我把响应写入SD卡。是这个原因吗?
private void deliveryResult(ResultCallback callback, final Request request) {
if (callback == null) callback = DEFAULT_RESULT_CALLBACK;
final ResultCallback resCallBack = callback;
deliveryResultCore(request, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
LogUtil.i(request.tag().toString(), "request fail " + e);
sendFailedStringCallback(request, e, resCallBack);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
MyResponse mrs = new MyResponse();
mrs.code = response.code();
mrs.content = response.body().string();
mrs.isSuccessful = response.isSuccessful();
mrs.accessToken = response.header("x-access-token");
LogUtil.i(request.tag().toString(), "request success ", mrs); // this will be wtite the log into sdcard
sendSuccessResultCallback(mrs, resCallBack);
} catch (Exception e) {
sendFailedStringCallback(response.request(), e, resCallBack);
}
}
});
}
后来我得到了W / art:暂停所有线程:9.234ms。是这个原因吗?
答案 0 :(得分:0)
您的CONNECTION_TIME_OUT变量可能太低。你必须增加超时之前的时间,例如[例如:60秒]。
超时的原因可能是服务器回复时间过长或互联网速度慢。