package com.fine47.http;

import android.util.Log;
import com.loopj.android.http.JsonHttpResponseHandler;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

class ResponseHandler extends JsonHttpResponseHandler {

  private Response response;
  private ActivityHttpClient client;

  public ResponseHandler(ActivityHttpClient client, Response response) {
    super();
    this.client = client;
    this.response = response;

    // If this is a long running operation, we'd like to run on the pool thread.
    setUsePoolThread(response.isLongRunning());
  }

  @Override
  public void onCancel() {
  }

  @Override
  public void onSuccess(
    int statusCode,
    Header[] headers,
    JSONObject result
  ) {
    if(ActivityHttpClient.isDebugging()) {
      Log.d(
        ActivityHttpClient.LOG_TAG,
        "Response JSON (Object): \n" + result.toString());
    }
    response.onSuccess(
      client.normalizeJson(result)
    );
    destroy();
  }

  @Override
  public void onSuccess(
    int statusCode,
    Header[] headers,
    JSONArray result
  ) {
    if(ActivityHttpClient.isDebugging()) {
      Log.d(
        ActivityHttpClient.LOG_TAG,
        "Response JSON (Array): \n" + result.toString());
    }
    response.onSuccess(
      client.normalizeJson(result)
    );
    destroy();
  }

  @Override
  public void onFailure(
    int statusCode,
    Header[] headers,
    String result,
    Throwable error
  ) {
    onFailure(
      statusCode,
      headers,
      error,
      (JSONObject)null
    );
  }

  @Override
  public void onFailure(
    int statusCode,
    Header[] headers,
    Throwable error,
    JSONObject result
  ) {
    response.onFailure(
      error,
      client.normalizeJson(result)
    );
    destroy();
  }

  @Override
  public void onFailure(
    int statusCode,
    Header[] headers,
    Throwable error,
    JSONArray result
  ) {
    response.onFailure(
      error,
      client.normalizeJson(result)
    );
    destroy();
  }

  private void destroy() {
    response = null;
    client = null;
  }
}

@@ -860,32 +860,67 @@
  (Object): %5Cn%22 +
+%0A        (null == result ? %22null%22 :
  result.toString
@@ -914,32 +914,33 @@
 sult.toString())
+)
 ;%0A    %7D%0A    resp
@@ -1252,16 +1252,51 @@
 ): %5Cn%22 +
+%0A        (null == result ? %22null%22 :
  result.
@@ -1302,24 +1302,25 @@
 .toString())
+)
 ;%0A    %7D%0A    
@@ -1741,32 +1741,247 @@
 ct result%0A  ) %7B%0A
+    if(ActivityHttpClient.isDebugging()) %7B%0A      Log.e(%0A        ActivityHttpClient.LOG_TAG,%0A        %22Failure while expecting JSON (Object) result: %5Cn%22 +%0A        (null == result ? %22null%22 : result.toString()));%0A    %7D%0A
     response.onF
@@ -2180,30 +2180,244 @@
 rray result%0A
-
   ) %7B%0A
+    if(ActivityHttpClient.isDebugging()) %7B%0A      Log.e(%0A        ActivityHttpClient.LOG_TAG,%0A        %22Failure while expecting JSON (Array) result: %5Cn%22 +%0A        (null == result ? %22null%22 : result.toString()));%0A    %7D%0A
     response

Fix possible NPE.