Created
November 26, 2011 09:53
-
-
Save FernandoEscher/1395390 to your computer and use it in GitHub Desktop.
Android JSONResponseHandler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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