[英]okHttp doesn't cancel execute
I am using: Andorid Studio. 我正在使用:Andorid Studio。 Okhttp 2.4.0 with AsyncTask.
使用AsyncTask的Okhttp 2.4.0。 But I can't cancel a request.
但是我无法取消请求。 Connected to server on wi-fi.
已连接到Wi-Fi上的服务器。 And if server is off then okHttp keeps trying to connect, and I can't cancel it.
如果服务器关闭,则okHttp会继续尝试连接,而我无法取消它。 time outs is working but i want to cancel before timeouts
超时正在工作,但我想在超时之前取消
private OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(30000, TimeUnit.MILLISECONDS);
after execute I press special cancel button in my api 执行后,我按我的api中的特殊取消按钮
public void onCancelClick(View v){
BaseApplication.getInstance().server.cancel();
synchProducts.cancel(true);
}
first line must stoped okHttp, second line stoped class extended AsyncTask 第一行必须停止okHttp,第二行必须停止类扩展AsyncTask
private static final String myTag = "AsyncCall";
public void cancel(){
client.cancel(myTag);
}
backGround in AsyncTask class AsyncTask类中的backGround
@Override
protected String doInBackground(Void... params) {
publishProgress(1);
String responseStr = sendProductHeaders();
//this performed after timeouts, and spit to my cancel okHttp
publishProgress(2);
if (responseStr != null && !isCancelled()){
ProductsList productsForSend = parseProducts(responseStr);
//correctly stoped thread
I didn't forget to use .tag
in the builder request 我没有忘记在构建器请求中使用
.tag
public Response post(String url, String json) {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.tag(myTag)
.url(url)
.post(body)
.build();
return call(request);
}
"call" is my method which makes the okHttp call “ call”是我的方法,可以调用okHttp
private Response call(Request request){
try {
return client.newCall(request).execute();
} catch (IOException e) {
Log.e("Aync call", "IO exception " + e.getMessage());
}
return null;
}
"Tags" is true, code in okHttp Library realy fires call.cancel(); “标记”是正确的,okHttp库中的代码确实会触发call.cancel();
for (Call call : executedCalls) {
if (Util.equal(tag, call.tag())) {
call.cancel();
}
}
method which is running Async 运行异步的方法
public void onRefreshProducts(View v){
progressLayout.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
synchProducts = new SynchProducts(activityCallBack);
synchProducts.execute();
}
"activityCallBack" is the interface I use when I call to my activity from AsyncTask class i don't want to use okHttp enqueue, but I can't insert a friendly progress bar in my app. 当我从AsyncTask类调用我的活动时,我不想使用okHttp入队,但是我无法在应用程序中插入友好的进度栏,因此会使用“ activityCallBack”接口。 Thanks!
谢谢!
Try updating the library to okhttp 2.5.0
and use the cancel() method. 尝试将库更新为
okhttp 2.5.0
并使用cancel()方法。 The ChangeLog for 2.5.0 mentions the following: 2.5.0的ChangeLog提到以下内容:
Call canceling is more reliable.
取消呼叫更为可靠。 We had a bug where a socket being connected wasn't being closed when the application used Call.cancel().
我们有一个错误,当应用程序使用Call.cancel()时,没有关闭正在连接的套接字。
Hopefully it will fix the issue. 希望它将解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.