title: Okhttp response callbacks on the main thread
body: <p>I have created a helper class to handle all of my http calls in my app. It is a simple singleton wrapper for okhttp that looks like this (I have omitted some unimportant parts):</p>
  
  <pre><code>public class HttpUtil {
  
      private OkHttpClient client;
      private Request.Builder builder;
  
      ...
  
      public void get(String url, HttpCallback cb) {
          call("GET", url, cb);
      }
  
      public void post(String url, HttpCallback cb) {
          call("POST", url, cb);
      }
  
      private void call(String method, String url, final HttpCallback cb) {
          Request request = builder.url(url).method(method, method.equals("GET") ? null : new RequestBody() {
              // don't care much about request body
              @Override
              public MediaType contentType() {
                  return null;
              }
  
              @Override
              public void writeTo(BufferedSink sink) throws IOException {
  
              }
          }).build();
  
          client.newCall(request).enqueue(new Callback() {
              @Override
              public void onFailure(Request request, Throwable throwable) {
                  cb.onFailure(null, throwable);
              }
  
              @Override
              public void onResponse(Response response) throws IOException {
                  if (!response.isSuccessful()) {
                      cb.onFailure(response, null);
                      return;
                  }
                  cb.onSuccess(response);
              }
          });
      }
  
  
      public interface HttpCallback  {
  
          /**
           * called when the server response was not 2xx or when an exception was thrown in the process
           * @param response - in case of server error (4xx, 5xx) this contains the server response
           *                 in case of IO exception this is null
           * @param throwable - contains the exception. in case of server error (4xx, 5xx) this is null
           */
          public void onFailure(Response response, Throwable throwable);
  
          /**
           * contains the server response
           * @param response
           */
          public void onSuccess(Response response);
      }
  
  }
  </code></pre>
  
  <p>Then, in my main activity, I use this helper class :</p>
  
  <pre><code>HttpUtil.get(url, new HttpUtil.HttpCallback() {
              @Override
              public void onFailure(Response response, Throwable throwable) {
                  // handle failure
              }
  
              @Override
              public void onSuccess(Response response) {
                  // &lt;-------- Do some view manipulation here
              }
          });
  </code></pre>
  
  <p><code>onSuccess</code> throws an exception when the code runs :</p>
  
  <blockquote>
    <p>android.view.ViewRootImpl$CalledFromWrongThreadException: Only the
    original thread that created a view hierarchy can touch its views.</p>
  </blockquote>
  
  <p>From my understanding, Okhttp callbacks run on the main thread so why do I get this error ? </p>
  
  <p>** Just as a side note, I have created <code>HttpCallback</code> interface to wrap Okhttp's <code>Callback</code> class because I wanted to change the behaviour of <code>onResponse</code> and <code>onFailure</code> so I could unite the logic of handling failed responses due to i/o exception and failed responses due to server problems.</p>
  
  <p>Thanks.</p>