gpt4 book ai didi

java - 如何使用 OkHttp Android 取消请求

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:40 33 4
gpt4 key购买 nike

我读了线程 OkHttpClient cannot cancel Call by tag但这对我不起作用。我有一个带有 TextWatcherEditText 来使用 Google PlacesAutocomplete 获取 Places 地址,所以输入的每个字符都是一个新请求,但是我输入的每个字符都需要取消之前的请求所以我没有收到它的返回,因为重要的是输入的最终地址,我这样做了:

Update!

Address.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(Address.getText().toString().length() > 3){
_Address = Address.getText().toString();
if(call != null){
call.cancel();
}

Request request = new Request.Builder()
.url(getPlaceAutoCompleteUrl(_Address))
.addHeader("content-type", "application/json")
.addHeader("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; google_sdk Build/MR1) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
.build();
Call call = new OkHttpClient().newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("Response",response.body().string());
PlacePredictions place = LoganSquare.parse(response.body().string(),PlacePredictions.class);
if(autoCompleteAdapter == null){
autoCompleteAdapter = new AutoCompleteAdapter(CustomPlaces.this);
recyclerView.setAdapter(autoCompleteAdapter);
autoCompleteAdapter.Add(place.getPlaces());
}else {
autoCompleteAdapter.Clear();
autoCompleteAdapter.Add(place.getPlaces());
autoCompleteAdapter.notifyDataSetChanged();
}
}
});
//call.cancel();
}

异常:

E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.app, PID: 2660
java.lang.IllegalStateException: closed
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:398)
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:392)
at okhttp3.internal.Util.bomAwareCharset(Util.java:449)
at okhttp3.ResponseBody.string(ResponseBody.java:174)
at com.ustork.CustomPlaces$2$1.onResponse(CustomPlaces.java:89)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

最佳答案

试试另一种方法来解决您的问题怎么样?你可以用 OkHttp 做这样的事情而不用 asyncTask:

Call call = new OkHttpClient().newCall(your request);
call.enqueue(new Callback() { // call off UI thread.
@Override
public void onFailure(Call call, IOException e) {
// this is where your call is cancelled: call.isCanceled()
}

@Override
public void onResponse(Call call, Response response) throws IOException {

}
});
call.cancel(); // this is the proper way to cancel an async call.

更新:

您应该注意到响应主体是一个一次性值,只能使用一次,您不应该调用response.body().string()两次。只需创建一个局部变量:responseString = response.body().string() 并仅使用它。

关于java - 如何使用 OkHttp Android 取消请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46470239/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com