2

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);
dvdciri
  • 441
  • 6
  • 19
  • Please take a look at my 2 answers [here](http://stackoverflow.com/questions/32375295/android-how-to-return-async-jsonobject-from-method-using-volley/32379539#32379539) and [here](http://stackoverflow.com/questions/32627089/post-request-json-file-passing-string-and-wait-for-the-response-volley/32627293#32627293) – BNK Oct 05 '15 at 21:55
  • Ok I see it, but is the same that I have before posting the answere here, i have always the respinse of the later REQUEST! – dvdciri Oct 05 '15 at 23:29

1 Answers1

1

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.

public interface VolleyCallback{
    void onSuccess(JSONObject string);
    void customOnSuccess(JSONObject string);
}

And your method to this

public static void RequestJsonToServer(boolean flag,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) {
                    if(flag){
                    callback.onSuccess(response);
                    }else{
                    callback.CustomOnSuccess(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);

}

Yes its reusable use onSucces where ever you have only one callback use both when you have more than one.

Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
  • Thank you very much, I'm looking right for that.. Can you please make me an example of that??? Thanks – dvdciri Oct 05 '15 at 11:31
  • But I've to do a lot of request and i would like to make a reusable callback, not one for each request.. – dvdciri Oct 05 '15 at 11:33
  • Thank you very much for the help! – dvdciri Oct 05 '15 at 11:40
  • And the usage is when I've to use one callback I'll pass true, instead for the other callback I'll pass false? – dvdciri Oct 05 '15 at 11:44
  • the code is proper it's not working because you are creating a new instance of request object. There should be only one request object per app. please modify your code accordingly[implement singelton pattern] then it will start working i posted the code after testing it – Aniruddha K.M Oct 05 '15 at 11:58
  • could you try after making these changes also refer https://www.youtube.com/watch?v=yhv8l9F44qo and this https://developer.android.com/training/volley/request.html – Aniruddha K.M Oct 05 '15 at 12:07
  • Okay, can you post some more code about the implementation of singleton pattern? thanks – dvdciri Oct 05 '15 at 12:09
  • I can't resolve this problem.. Please can you show me how to create a singleton pattern and on to use it when i make the request? please – dvdciri Oct 05 '15 at 12:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91388/discussion-between-dvdciri-and-war-hero). – dvdciri Oct 05 '15 at 12:46