Skip to content

Instantly share code, notes, and snippets.

@FernandoEscher
Created November 26, 2011 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FernandoEscher/1395390 to your computer and use it in GitHub Desktop.
Save FernandoEscher/1395390 to your computer and use it in GitHub Desktop.
Android JSONResponseHandler
public class JSONResponseHandler implements ResponseHandler<Object>{
final static String TAG = "JSONResponseHandler";
@Override
public Object handleResponse(HttpResponse response)
throws HttpResponseException, IOException {
StatusLine statusLine = response.getStatusLine();
HttpEntity entity = response.getEntity();
if(statusLine.getStatusCode() >= 300){
Log.i(TAG, "HTTP Status Code: " + statusLine.getStatusCode());
throw new HttpResponseException(statusLine.getStatusCode(),
entity==null?statusLine.getReasonPhrase():EntityUtils.toString(entity));
}
String data = entity==null?null:EntityUtils.toString(entity);
try {
return data==null?null:new JSONObject(data);
} catch (ParseException e) {
e.printStackTrace();
} catch (JSONException e){
try{
return new JSONArray(data);
}catch(Exception ex){
Log.i(TAG, data);
ex.printStackTrace();
}
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment