2323import org .json .JSONObject ;
2424import org .json .JSONTokener ;
2525
26+ import android .os .Message ;
27+
2628/**
2729 * Used to intercept and handle the responses from requests made using
2830 * {@link AsyncHttpClient}, with automatic parsing into a {@link JSONObject}
3638 * parent class.
3739 */
3840public class JsonHttpResponseHandler extends AsyncHttpResponseHandler {
41+ protected static final int SUCCESS_JSON_MESSAGE = 100 ;
42+
3943 //
4044 // Callbacks to be overridden, typically anonymously
4145 //
@@ -60,20 +64,33 @@ public void onSuccess(JSONArray response) {}
6064
6165 // Utility methods
6266 @ Override
63- protected void handleSuccessMessage (String responseBody ) {
64- super .handleSuccessMessage (responseBody );
67+ protected void handleMessage (Message msg ) {
68+ switch (msg .what ){
69+ case SUCCESS_JSON_MESSAGE :
70+ handleSuccessJsonMessage (msg .obj );
71+ break ;
72+ default :
73+ super .handleMessage (msg );
74+ }
75+ }
76+
77+ protected void handleSuccessJsonMessage (Object jsonResponse ) {
78+ if (jsonResponse instanceof JSONObject ) {
79+ onSuccess ((JSONObject )jsonResponse );
80+ } else if (jsonResponse instanceof JSONArray ) {
81+ onSuccess ((JSONArray )jsonResponse );
82+ } else {
83+ onFailure (new JSONException ("Unexpected type " + jsonResponse .getClass ().getName ()));
84+ }
85+ }
6586
87+ @ Override
88+ protected void sendSuccessMessage (String responseBody ) {
6689 try {
6790 Object jsonResponse = parseResponse (responseBody );
68- if (jsonResponse instanceof JSONObject ) {
69- onSuccess ((JSONObject )jsonResponse );
70- } else if (jsonResponse instanceof JSONArray ) {
71- onSuccess ((JSONArray )jsonResponse );
72- } else {
73- throw new JSONException ("Unexpected type " + jsonResponse .getClass ().getName ());
74- }
91+ sendMessage (obtainMessage (SUCCESS_JSON_MESSAGE , jsonResponse ));
7592 } catch (JSONException e ) {
76- onFailure (e , responseBody );
93+ sendFailureMessage (e , responseBody );
7794 }
7895 }
7996
0 commit comments