In my application I need to send multiple volley request in a sequence. I've create a common listener for dealing with the volley response.
public interface RequestCallBack {
void onSuccess(JSONObject jsonObject, String tag)
void OnError(String message);
}
And registered this callback using method:
public void setOnResponseListener (RequestCallBack onResponseListener) {
this.onResponseListener = onResponseListener;
}
I've created a common method in which volley request is handle.
public void getResponse(String tag, String url) {
JsonObjectRequest jsonObjectRequest;
try {
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
url,null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
mStatusCode = response.optInt("status_code");
mBody = response.optJSONObject("body");
if (mStatusCode != 0 && mStatusCode == 201) {
onResponseListener.onSuccess(mBody, (String) jsonObjectRequest.getTag());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
onResponseListener.OnError(displayVolleyError(error));
}
}) {
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
volleyError = new VolleyError(new String(volleyError.networkResponse.data));
}
return volleyError;
}
};
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
REQUEST_TIMEOUT_MS,
NO_OF_RETRY_ATTEMPTS,
BACK_OF_MULTIPLIER));
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjectRequest, tag);
} catch (Exception e) {
e.printStackTrace();
}
}
I've called this method from a class as:
classObject.getResponse("request1", firstUrl);
classObject.getResponse("request2", secondUrl);
And I've overrided the RequestCallBack interface methods. But Inside the onSuccess Method each time the tag of 2nd request is returned.
@Override
public void onSuccess(JSONObject jsonObject, String tag) {
Log.d("Class", "tag: "+tag); // Always returns the "request2"
// Will check service response according to tag
// but didn't get the right tag.
}
@Override
public void OnError(String message) {
}
Can anyone suggest me here how to resolve this issue.
Thanks in advance.