Volley not calling getParams() for second time
Asked Answered
S

1

2

I am trying to post login parameters to my server using Volley in Android.For first time login it send params, but after i logout and try to login again, it won't call getparams, instead it sends the previous login params to the server.

I tried with log statements,On first time it prints log statemant, but on the second time it won't

StringRequest stringRequest=new StringRequest(Request.Method.POST,LOGIN_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (response!=null){
                        Log.d("loginResponse", response);
                        try {
                            JSONObject object= new JSONObject(response);
                            uid=object.getString(KEY_UR_ID);
                            firstName=object.getString(KEY_F_NAME);
                            lastName=object.getString(KEY_L_NAME);
                            fullName=firstName+" "+lastName;
                            email=object.getString(KEY_EMAIL);
                            phone=object.getString(KEY_PHONE);
                            dob=object.getString(KEY_DOB);
                            gender=object.getString(KEY_GENDER);
                            userType=object.getString(KEY_UTYPE);
                            address=object.getString(KEY_U_ADDRESS);
                            pincode=object.getString(KEY_U_PINCODE);

                            // Inserting row in users table
                            db.addUser(uid, firstName, lastName,email,phone,gender,userType,address,pincode);
                            // user successfully logged in
                            // Create login session
                            session.setLogin(true);
                            loginProgress.dismiss();
                            onBackPressed();

                        } catch (JSONException e) {
                            loginProgress.dismiss();
                            Toast.makeText(getApplicationContext(),"Invalid username/password",Toast.LENGTH_LONG).show();
                            e.printStackTrace();
                        }

                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            String message = null;
            if (error instanceof NetworkError) {
                message = "Cannot connect to Internet...Please check your connection!";
            } else if (error instanceof ServerError) {
                message = "The server could not be found. Please try again after some time!!";
            } else if (error instanceof AuthFailureError) {
                message = "Invalid username/password";
            } else if (error instanceof ParseError) {
                message = "Parsing error! Please try again after some time!!";
            } else if (error instanceof TimeoutError) {
                message = "Connection TimeOut! Please check your internet connection.";
            }
            Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();

            loginProgress.dismiss();
        }
    })
    {
        @Override
        protected Map<String,String> getParams(){

            Map<String,String> params = new HashMap<String, String>();

            //This does not appear in the log for second time login
            Log.d("Params","Does it assign params?") ;
            params.put(KEY_EMAIL, inputEmail);
            params.put(KEY_PASSWORD, inputPassword);
            return params;
        }


        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> header = new HashMap<String, String>();
            header.put("Content-Type","application/x-www-form-urlencoded");
            return header;
        }
        @Override
        protected String getParamsEncoding() {
            return "utf-8";
        }
    };
    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
Supertax answered 5/1, 2017 at 14:22 Comment(4)
Are you creating request every time, check by putting logs or debugging in other places also i think you are not making the request second time, since half code is present every thing else looks fine,Knighthead
What your error log?Jackscrew
at first put Log.d("...") in getParams() to ensure that method called , the if it called , the use breakpoint for first line of getParams() method and trace lines , may there are one exception and method throws out , too put Log.d("...") in all catch exception to see what happened.Bensky
@ piyush, on second time login i get the string response but it is same as the firstSupertax
M
5

I had this problem as well. it happened because volley params cache. Try to clean the cache with command: getCache().clear(); i.e.:

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.getCache().clear();
requestQueue.add(stringRequest);
Macguiness answered 3/2, 2017 at 1:51 Comment(1)
Had the same problem and this works perfect! Thanks a lot!Dunnock

© 2022 - 2024 — McMap. All rights reserved.