I've added a callback on the volley request and, I have two request started from 2 different activity. When I perform the first request, then the second return the response of the first..
This is my request and callback:
public static void RequestJsonToServer(Context ctx, final JSONObject params, final VolleyCallback callback){
MySingleVolley.getInstance(ctx).
getRequestQueue();
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,ctx.getString(R.string.defaultServerRequestUrl),params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
callback.onSuccess(response);
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("REQUEST_JSON_TO_SERVER", "Error: " + error);
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return headers;
}
};
MySingleVolley.getInstance(ctx).addToRequestQueue(jsObjRequest);
}
public interface VolleyCallback{
void onSuccess(JSONObject string);
}
And this is one of the two starting request:
Global.RequestJsonToServer(getActivity(), jsonObject, new Global.VolleyCallback() {
@Override
public void onSuccess(JSONObject result) {
Toast.makeText(getActivity(),result.toString(), Toast.LENGTH_LONG).show();
}
});
I hope someone can help me
Thanks
Edit: I change it like this
Global.RequestJsonToServer(getApplicationContext(), jsonObject, new Global.VolleyCallback() {
@Override
public void onSuccess(JSONObject result) {
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onSuccessCustom(JSONObject string) {
}
}, true);
And the other one with false.. But maybe is the wrong way to use it..I would like to have one single callback and reuse it, not switch between two callbacks
MY SOLUTION
I've found my own solution, the problem wasn't in the callback but was in the volley request. The response of each request was cached and i don't know why, it will be return the wrong always the wrong response.
I've just added this before adding request to queue:
jsObjRequest.setShouldCache(false);
This is happening because your callback method is common[onSuccess(...)]why don't you write 2 call backs and based on the condition use the required callback. To implement it write 2 methods in your interface and pass some sort of flag to choose callback.
Change your interface to this.
And your method to this
Yes its reusable use onSucces where ever you have only one callback use both when you have more than one.