Skip to content

Commit 53d5bd4

Browse files
committed
add FriendlyHttpResponseHandler, which dispatches text/json responses to
appropriate handlers, and provides noisy (Toasts to user) error handlers by default
1 parent 81ce21f commit 53d5bd4

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
Android Asynchronous Http Client
3+
"Friendly" HTTP Response handler detects response content type and
4+
dispatches to appropriate success and failure handlers.
5+
6+
Also, default error handlers provide noisy error messages to users by default,
7+
rather than doing nothing. eg., if the server goes down, pop up a Toast saying
8+
as much rather than calling an empty error handler.
9+
10+
*/
11+
12+
package com.loopj.android.http;
13+
14+
15+
import org.apache.http.Header;
16+
import org.json.JSONArray;
17+
import org.json.JSONException;
18+
import org.json.JSONObject;
19+
import org.json.JSONTokener;
20+
21+
import android.content.Context;
22+
23+
import android.widget.Toast;
24+
25+
public class FriendlyHttpResponseHandler extends
26+
com.loopj.android.http.AsyncHttpResponseHandler {
27+
28+
Context context;
29+
30+
public FriendlyHttpResponseHandler(Context startContext) {
31+
super();
32+
context = startContext;
33+
}
34+
35+
@Override
36+
public void onFailure(Throwable e) {
37+
String errorDescription = "An unknown error occurred.";
38+
Toast toast = Toast.makeText(context, errorDescription,
39+
Toast.LENGTH_SHORT);
40+
toast.show();
41+
}
42+
43+
public void onFailure(Throwable e, JSONObject errorResponse) {
44+
String errorDescription = "An unknown error occurred.";
45+
try {
46+
if (errorResponse.has("error")) {
47+
errorDescription = errorResponse.getString("error");
48+
}
49+
} catch (JSONException e1) {
50+
e1.printStackTrace();
51+
}
52+
Toast toast = Toast.makeText(context, errorDescription,
53+
Toast.LENGTH_SHORT);
54+
toast.show();
55+
}
56+
57+
@Override
58+
public void onFailure(Throwable e, String errorResponse) {
59+
Toast toast = Toast
60+
.makeText(context, errorResponse, Toast.LENGTH_SHORT);
61+
toast.show();
62+
}
63+
64+
public void onFailure(Throwable e, JSONArray errorResponse) {
65+
String errorDescription = "An unknown error occurred.";
66+
Toast toast = Toast.makeText(context, errorDescription,
67+
Toast.LENGTH_SHORT);
68+
toast.show();
69+
}
70+
71+
public void onSuccess(JSONObject response) {
72+
}
73+
74+
public void onSuccess(JSONArray response) {
75+
}
76+
77+
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
78+
onSuccess(response);
79+
}
80+
81+
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
82+
onSuccess(response);
83+
}
84+
85+
protected void handleFailureMessage(Throwable e, Header[] headers,
86+
String responseBody) {
87+
if (headers != null) {
88+
for (Header header : headers) {
89+
// XXX Find a header parsing library
90+
if (header.getName().startsWith("Content-Type")
91+
&& header.getValue().contains("application/json")) {
92+
try {
93+
Object jsonResponse = parseResponse(responseBody);
94+
handleFailureJsonMessage(headers, jsonResponse);
95+
return;
96+
} catch (JSONException e1) {
97+
onFailure(e1, responseBody);
98+
}
99+
}
100+
}
101+
}
102+
onFailure(e, responseBody);
103+
}
104+
105+
protected void handleFailureJsonMessage(Header[] headers,
106+
Object jsonResponse) {
107+
if (jsonResponse instanceof JSONObject) {
108+
JSONObject obj = (JSONObject) jsonResponse;
109+
try {
110+
if (obj.has("error") && (obj.getString("error").length() > 0)) {
111+
onFailure(
112+
new JSONException("Error: ".concat(obj
113+
.getString("error"))), obj);
114+
}
115+
} catch (JSONException e) {
116+
// No error found
117+
}
118+
}
119+
}
120+
121+
protected void handleSuccessMessage(int statusCode, Header[] headers,
122+
String responseBody) {
123+
for (Header header : headers) {
124+
if (header.getName().startsWith("Content-Type")
125+
&& header.getValue().contains("application/json")) {
126+
try {
127+
Object jsonResponse = parseResponse(responseBody);
128+
handleSuccessJsonMessage(statusCode, headers, jsonResponse);
129+
} catch (JSONException e) {
130+
handleFailureMessage(e, headers, responseBody);
131+
}
132+
}
133+
}
134+
onSuccess(statusCode, headers, responseBody);
135+
}
136+
137+
protected void handleSuccessJsonMessage(int statusCode, Header[] headers,
138+
Object jsonResponse) {
139+
if (jsonResponse instanceof JSONObject) {
140+
onSuccess(statusCode, headers, (JSONObject) jsonResponse);
141+
} else if (jsonResponse instanceof JSONArray) {
142+
onSuccess(statusCode, headers, (JSONArray) jsonResponse);
143+
} else {
144+
onFailure(new JSONException("Unexpected type "
145+
+ jsonResponse.getClass().getName()), (JSONObject) null);
146+
}
147+
}
148+
149+
protected Object parseResponse(String responseBody) throws JSONException {
150+
Object result = null;
151+
// trim the string to prevent start with blank, and test if the string
152+
// is valid JSON, because the parser don't do this :(. If Json is not
153+
// valid this will return null
154+
responseBody = responseBody.trim();
155+
if (responseBody.startsWith("{") || responseBody.startsWith("[")) {
156+
result = new JSONTokener(responseBody).nextValue();
157+
}
158+
if (result == null) {
159+
result = responseBody;
160+
}
161+
return result;
162+
}
163+
}

0 commit comments

Comments
 (0)