ivan
ivan

Reputation: 1205

Android Volley request works in emulator but not in device

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;
            }
        };

Upvotes: 2

Views: 459

Answers (1)

ivan
ivan

Reputation: 1205

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;
}

Upvotes: 1

Related Questions