【问题标题】:Volley not calling getParams() for second timeVolley 第二次没有调用 getParams()
【发布时间】:2017-01-05 14:22:52
【问题描述】:

我正在尝试使用 Android 中的 Volley 将登录参数发布到我的服务器。第一次登录时它会发送参数,但在我注销并尝试再次登录后,它不会调用 getparams,而是发送以前的登录参数到服务器。

我尝试使用日志语句,第一次打印日志语句,但第二次不会

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

【问题讨论】:

  • 您是否每次都创建请求,通过在其他地方放置日志或调试进行检查,我认为您没有第二次发出请求,因为存在一半代码,其他一切看起来都很好,
  • 你的错误日志是什么?
  • 先把Log.d("...")放在getParams()中保证方法被调用,如果调用了,breakpoint作为getParams()方法的第一行和跟踪行,可能有一个异常和方法抛出,也把Log.d("...")放在所有catch异常中看看发生了什么。
  • @piyush,在第二次登录时我得到了字符串响应,但它与第一​​次相同

标签: android android-volley


【解决方案1】:

我也有这个问题。 发生这种情况是因为凌空参数缓存。 尝试使用命令清理缓存:getCache().clear(); 即:

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.getCache().clear();
requestQueue.add(stringRequest);

【讨论】:

  • 遇到了同样的问题,这很完美!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
  • 2023-03-19
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
相关资源
最近更新 更多