I created this and managed like the following way please have a look, hope it will be useful to you
public class APIManager {
public static void createRequest(Context c, String requestTag,
String endPoint, List<NameValuePair> params,
final OnRequestCompletedListener listener,
TransParentProgressDialog pd) {
ServerDetails serverDetails = new ServerDetails(c, endPoint, params);
JsonObjectRequest request = new JsonObjectRequest(Method.GET,
serverDetails.getQueryUrl(), null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
listener.onRequestCompleted(response);
}
}, getErrorListener(c, pd)) {
};
AppController.getInstance().addToRequestQueue(request, requestTag);
}
public static ErrorListener getErrorListener(final Context c,
final TransParentProgressDialog pd, final TextView tvEmpty,
final String errorText) {
Response.ErrorListener listener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (pd != null && pd.isShowing()) {
pd.dismiss();
}
if (tvEmpty != null) {
tvEmpty.setText(errorText);
}
MyDialog dialog;
Log.d("volley-error", error.toString());
if (error instanceof TimeoutError) {
dialog = new MyDialog(c, "Server Timeout");
dialog.show();
return;
} else if (error instanceof NoConnectionError) {
dialog = new MyDialog(c, "No Connection or Invalid Url");
dialog.show();
return;
} else if (error instanceof ServerError) {
NetworkResponse response = error.networkResponse;
if (response != null) {
// int statusCode = response.statusCode;
byte[] data = response.data;
if (data != null) {
String str = new String(data);
try {
JSONObject object = new JSONObject(str);
Log.d("error response", object.toString());
if (object.has("errors")) {
JSONArray errors = object
.getJSONArray("errors");
JSONObject errorObject = errors
.getJSONObject(0);
dialog = new MyDialog(c, "Error!",
errorObject.getString("message"));
dialog.show();
} else {
dialog = new MyDialog(c, "Error!",
object.toString());
dialog.show();
}
} catch (JSONException e) {
e.printStackTrace();
dialog = new MyDialog(c, "Error!", "Error");
dialog.show();
}
} else {
dialog = new MyDialog(c, "Server Error");
dialog.show();
}
} else {
dialog = new MyDialog(c, "Server Error");
dialog.show();
}
} else if (error instanceof NetworkError) {
NetworkResponse response = error.networkResponse;
if (response != null) {
// int statusCode = response.statusCode;
byte[] data = response.data;
if (data != null) {
String str = new String(data);
try {
JSONObject object = new JSONObject(str);
Log.d("error response", object.toString());
if (object.has("errors")) {
JSONArray errors = object
.getJSONArray("errors");
JSONObject errorObject = errors
.getJSONObject(0);
dialog = new MyDialog(c, "Error!",
errorObject.getString("message"));
dialog.show();
} else {
dialog = new MyDialog(c, "Error!",
object.toString());
dialog.show();
}
} catch (JSONException e) {
e.printStackTrace();
dialog = new MyDialog(c, "Error!", "Error");
dialog.show();
}
} else {
dialog = new MyDialog(c, "Network Error");
dialog.show();
}
} else {
dialog = new MyDialog(c, "Network Error");
dialog.show();
}
} else if (error instanceof ParseError) {
dialog = new MyDialog(c, "Parse Error");
dialog.show();
} else if (error instanceof AuthFailureError) {
NetworkResponse response = error.networkResponse;
if (response != null) {
// int statusCode = response.statusCode;
byte[] data = response.data;
if (data != null) {
String str = new String(data);
try {
JSONObject object = new JSONObject(str);
Log.d("error response", object.toString());
if (object.has("errors")) {
JSONArray errors = object
.getJSONArray("errors");
JSONObject errorObject = errors
.getJSONObject(0);
dialog = new MyDialog(c, "Error!",
errorObject.getString("message"));
dialog.show();
} else {
dialog = new MyDialog(c, "Error!",
object.toString());
dialog.show();
}
} catch (JSONException e) {
e.printStackTrace();
dialog = new MyDialog(c, "Error!", "Error");
dialog.show();
}
} else {
dialog = new MyDialog(c, "Error!", "Error");
dialog.show();
}
} else {
dialog = new MyDialog(c, "Error connecting server");
dialog.show();
}
}
}
};
return listener;
}
}
And the interface for call back on request completed is
public interface OnRequestCompletedListener {
public void onRequestCompleted(JSONObject response);
}
while(busy){ //do something}
– Noise