title: How to cancel a request with OkHttp Android
body: <p>I read the thread <a href="https://stackoverflow.com/questions/35964663/okhttpclient-cannot-cancel-call-by-tag">OkHttpClient cannot cancel Call by tag</a> but it's not working for me. I have a <code>EditText</code> with <code>TextWatcher</code> to get Places address using Google PlacesAutocomplete, so each char typed is a new request, but each char that I type i need to cancel the previous request so I don't receive the return of it, cu'z what matters is the final Address typed, I did this :</p>
  
  <blockquote>
    <p>Update!</p>
  </blockquote>
  
  <pre><code>Address.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              if(Address.getText().toString().length() &gt; 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();
              }
  </code></pre>
  
  <p>The Exception : </p>
  
  <pre><code>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)
  </code></pre>