2

I have a volley StringRequest using the POST method, the request is able to connect to the server in both device and emulator but, the request needs to have the parameters in the correct order for it to work, and for some reason, the emulator does send those parameters in order but the device does not.

Why is this? Is there something i can do to avoid this?

Debug screenshot: enter image description here

My StringRequest:

StringRequest xx = new StringRequest(Request.Method.POST, getAjaxUrlForFunction("Login"), new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.w("RESPONSE-=",response);
                callback.onSucces(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                callback.onError(error);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> paramss = new HashMap<String, String>();
                paramss.put("funcion","login");
                paramss.put("ajax_request","controller");
                paramss.put("args[0]", name);
                paramss.put("args[1]", password);
                return paramss;
            }
        };
ivan
  • 1,177
  • 8
  • 23
  • 1
    `HashMap` does not maintain order of insertion so not sure why you would expect as much; a LinkedHashMap does however: https://stackoverflow.com/a/10710205/2711811. –  Nov 20 '19 at 18:02
  • @Andy I see, i was unaware of this, however, i don't think i can use LinkedHashMap for the params in a volley request can i? – ivan Nov 20 '19 at 18:03
  • It extends `HashMap` so I don't see why not. –  Nov 20 '19 at 18:04
  • I was just realizing that, thanks for the quick response, you can post that as an answer if you'd like to. – ivan Nov 20 '19 at 18:06
  • Give it a try and if it works post your code as solution - would make a good future reference. –  Nov 20 '19 at 18:07
  • It is working now, will go ahead and do that, thanks for the help @Andy – ivan Nov 20 '19 at 18:08

1 Answers1

1

As noted by Andy and explained Here HashMaps don't retain order but LinkedHashMaps retains the insertion order of the items, so the only reason it worked on emulator and didn't on device was pure luck.

Working request params after using LinkedHashMap

@Override
protected Map<String, String> getParams() throws AuthFailureError {
    //Map<String,String> paramss = new HashMap<String, String>();
    LinkedHashMap<String,String> paramss = new LinkedHashMap<>();
    paramss.put("funcion","login");
    paramss.put("ajax_request", "controller");
    paramss.put("args[0]", name);
    paramss.put("args[1]", password);
    Log.w("PARAMETERS",paramss.toString());
    return paramss;
}
ivan
  • 1,177
  • 8
  • 23