Skip to content

Commit d1ae97a

Browse files
committed
use alerts, rather than toasts in FriendlyHttpResponseHandler
If notifications are disabled for an application, toasts are also squelched, leaving important error messages on the floor. One possible thing to look out for is that you currently pass a single Context to the handler. Previously, you could use the application's context. This meant if you got the async response when viewing another activity, it could still be handled as the application Context still existed. Now, you have to use an activity's context, which means if the response returns when viewing another activity, you'll get an exception. A workaround would be to use 2 contexts, one for the application to handle the response at all costs, and another for the activity to display the error when possible (if the activity still exists). We'll see if that's necessary...
1 parent 513b348 commit d1ae97a

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

src/com/loopj/android/http/FriendlyHttpResponseHandler.java

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
dispatches to appropriate success and failure handlers.
55
66
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.
7+
rather than doing nothing. eg., if the server goes down, pop up a dialog
8+
saying as much rather than calling an empty error handler.
99
1010
*/
1111

@@ -18,9 +18,9 @@
1818
import org.json.JSONObject;
1919
import org.json.JSONTokener;
2020

21+
import android.app.AlertDialog;
2122
import android.content.Context;
22-
23-
import android.widget.Toast;
23+
import android.content.DialogInterface;
2424

2525
public class FriendlyHttpResponseHandler extends
2626
com.loopj.android.http.AsyncHttpResponseHandler {
@@ -35,9 +35,19 @@ public FriendlyHttpResponseHandler(Context startContext) {
3535
@Override
3636
public void onFailure(Throwable e) {
3737
String errorDescription = "An unknown error occurred.";
38-
Toast toast = Toast.makeText(context, errorDescription,
39-
Toast.LENGTH_SHORT);
40-
toast.show();
38+
AlertDialog.Builder builder = new AlertDialog.Builder(
39+
context);
40+
builder.setTitle("Alert");
41+
builder.setMessage(errorDescription);
42+
builder.setPositiveButton("OK",
43+
new DialogInterface.OnClickListener() {
44+
@Override
45+
public void onClick(DialogInterface dialog,
46+
int which) {
47+
dialog.dismiss();
48+
}
49+
});
50+
builder.create().show();
4151
}
4252

4353
public void onFailure(Throwable e, JSONObject errorResponse) {
@@ -49,23 +59,53 @@ public void onFailure(Throwable e, JSONObject errorResponse) {
4959
} catch (JSONException e1) {
5060
e1.printStackTrace();
5161
}
52-
Toast toast = Toast.makeText(context, errorDescription,
53-
Toast.LENGTH_SHORT);
54-
toast.show();
62+
AlertDialog.Builder builder = new AlertDialog.Builder(
63+
context);
64+
builder.setTitle("Alert");
65+
builder.setMessage(errorDescription);
66+
builder.setPositiveButton("OK",
67+
new DialogInterface.OnClickListener() {
68+
@Override
69+
public void onClick(DialogInterface dialog,
70+
int which) {
71+
dialog.dismiss();
72+
}
73+
});
74+
builder.create().show();
5575
}
5676

5777
@Override
5878
public void onFailure(Throwable e, String errorResponse) {
59-
Toast toast = Toast
60-
.makeText(context, errorResponse, Toast.LENGTH_SHORT);
61-
toast.show();
79+
AlertDialog.Builder builder = new AlertDialog.Builder(
80+
context);
81+
builder.setTitle("Alert");
82+
builder.setMessage(errorResponse);
83+
builder.setPositiveButton("OK",
84+
new DialogInterface.OnClickListener() {
85+
@Override
86+
public void onClick(DialogInterface dialog,
87+
int which) {
88+
dialog.dismiss();
89+
}
90+
});
91+
builder.create().show();
6292
}
6393

6494
public void onFailure(Throwable e, JSONArray errorResponse) {
6595
String errorDescription = "An unknown error occurred.";
66-
Toast toast = Toast.makeText(context, errorDescription,
67-
Toast.LENGTH_SHORT);
68-
toast.show();
96+
AlertDialog.Builder builder = new AlertDialog.Builder(
97+
context);
98+
builder.setTitle("Alert");
99+
builder.setMessage(errorDescription);
100+
builder.setPositiveButton("OK",
101+
new DialogInterface.OnClickListener() {
102+
@Override
103+
public void onClick(DialogInterface dialog,
104+
int which) {
105+
dialog.dismiss();
106+
}
107+
});
108+
builder.create().show();
69109
}
70110

71111
public void onSuccess(JSONObject response) {
@@ -161,3 +201,4 @@ protected Object parseResponse(String responseBody) throws JSONException {
161201
return result;
162202
}
163203
}
204+

0 commit comments

Comments
 (0)