+-

我正在使用OkHttp,并想在特定的api调用上禁用连接重试.这是正确的方法吗?
mMyGlobalClient = new OkHttpClient();
....
public void makeConnection(...) {
OkHttpClient client = null;
if (disableRetries) {
OkHttpClient clone = mMyGlobalClient.clone();
clone.setRetryOnConnectionFailure(false);
client = clone;
} else {
client = mMyGlobalClient;
}
client.newCall(...);
}
这个想法来自这篇文章:
https://github.com/square/okhttp/pull/1259#issue-53157152
Most applications won’t want to disable retry globally. Instead, use
clone() to get an OkHttpClient for a specific, non-idempotent request,
then configure that client with the setting.
我要对此一次呼叫禁用重试的原因是,如果服务器两次对其进行处理,则它可能具有破坏性:
https://github.com/square/okhttp/pull/1259#issuecomment-68430264
谢谢
最佳答案
是的,这是正确的方法.
顺便说一句,如果您不介意的话,可以写得更简单
mMyGlobalClient = new OkHttpClient();
....
public void makeConnection(...) {
OkHttpClient client = null;
if (disableRetries) {
client = mMyGlobalClient.clone();
client.setRetryOnConnectionFailure(false);
} else {
client = mMyGlobalClient;
}
client.newCall(...);
}
点击查看更多相关文章
转载注明原文:android-如何禁用与OkHttp的一个连接的重试? - 乐贴网