title: Android Listener pattern vs Callback pattern
body: <p>When I'm working on networking functions in OkHttp, there are mainly 2 patterns I come across with:</p>
  
  <ol>
  <li>Listener pattern</li>
  <li>Callback pattern</li>
  </ol>
  
  <p><strong>Listener Pattern example:</strong></p>
  
  <pre><code>// Listener class
  public interface NetworkListener {
      void onFailure(Request request, IOException e);
      void onResponse(Response response);
  }
  
  // NetworkManager class
  public class NetworkManager {
      static String TAG = "NetworkManager";
      public NetworkListener listener;
      OkHttpClient client = new OkHttpClient();
      public void setListener(NetworkListener listener) {
          this.listener = listener;
      }
      void post(String url, JSONObject json) throws IOException {
          //RequestBody body = RequestBody.create(JSON, json);
          try {
              JSONArray array = json.getJSONArray("d");
              RequestBody body = new FormEncodingBuilder()
                      .add("m", json.getString("m"))
                      .add("d", array.toString())
                      .build();
              Request request = new Request.Builder()
                      .url(url)
                      .post(body)
                      .build();
  
              // Asynchronous Mode
              client.newCall(request).enqueue(new Callback() {
                  @Override
                  public void onFailure(Request request, IOException e) {
                      Log.e(TAG, e.toString());
                      if(listener != null) {
                          listener.onFailure(request, e);
                      }
                  }
  
                  @Override
                  public void onResponse(Response response) throws IOException {
                      Log.w(TAG, response.body().string());
                      if(listener != null) {
                          listener.onResponse(response);
                      }
  
                  }
              });
          } catch (JSONException jsone) {
              Log.e(TAG, jsone.getMessage());
          }
      }
  }
  
  // In the Activity
  NetworkManager manager = new NetworkManager();
  manager.setListener(this);
  try {
    requestState = RequestState.REQUESTING;
    manager.post("http://www.example.com/api.php", reqObj);
  } catch(IOException ioe) {
    Log.e(TAG, ioe.getMessage());
  }
  </code></pre>
  
  <p><strong>Callback Pattern example:</strong></p>
  
  <pre><code>// in onCreate
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      try {
          doGET(new Callback() {
              @Override
              public void onFailure(Request request, IOException e) {
                  Log.d("OkHttp", "Shit happens");
              }
  
              @Override
              public void onResponse(Response response) throws IOException {
                  if (response.isSuccessful()) {
                      String strResponse = response.body().string();
                      Gson gson = new Gson();
                      Wrapper wrapper = gson.fromJson(strResponse, Wrapper.class);
                      Log.d("OkHttp", wrapper.getListContents());
                  } else {
                      Log.d("OkHttp", "Request not successful");
                  }
              }
          });
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  
  Call doGET(Callback callback) throws IOException {
      // Start Network Request
      OkHttpClient client = new OkHttpClient();
      Request request = new Request.Builder().url("http://www.example.com/api.php").build();
      Call call  = client.newCall(request);
      call.enqueue(callback);
      return call;
  }
  </code></pre>
  
  <p>What are the advantages and disadvantages of using the 2 patterns above?</p>