有没有办法找到一个请求是否仍在执行中? 如果一个请求仍在执行但尚未完成,我想取消请求。
// fetch Results
private void fetchResults(CharSequence s) {
if (s.toString().length() > 3){
mSearchResult.clear();
mAdapter.notifyDataSetChanged();
HashMap hashMap = new HashMap();
hashMap.put("q", s.toString());
PrefManager pref = new PrefManager(SearchActivity.this);
String token = pref.getAuthenticationToken();
Call<AppointmentsModel> call = mAPIService.getResults("Token " + token, hashMap);
call.enqueue(new Callback<AppointmentsModel>() {
@Override
public void onResponse(Call<AppointmentsModel> call, Response<AppointmentsModel> response) {
List<Result> resultList = response.body().getResult();
mSearchResult.addAll(resultList);
mAdapter.notifyDataSetChanged();
showNoSearchResults();
}
@Override
public void onFailure(Call<AppointmentsModel> call, Throwable t) {
if (BuildConfig.DEBUG) Log.d(TAG, "onFailureRetrofit: " + t.toString());
}
});
}else{
showNoSearchResults();
}
}
我想在一个请求已经起作用时取消通话。你能告诉我我应该在哪里做同样的事情。谢谢。
答案 0 :(得分:1)
将其设置为全局:
Call<AppointmentsModel> call;
public void afterTextChanged (Editable s) {
if(call != null && call.isExecuted()) {
call.cancel();
}else{
//Your network call
}
}