【问题标题】:Getting AuthFailure Error when using StringRequest using Volley使用 Volley 使用 StringRequest 时出现 AuthFailure 错误
【发布时间】:2021-10-04 01:14:25
【问题描述】:

我发送 Volley StringRequest 如下

    StringRequest getRequest = new StringRequest(Request.Method.GET, myUrl,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    try {
                        if (response!=null)
                        {
                           //do some work
                        }
                    }
                    catch (Exception e)
                    {
                        Log.d("error",e.getMessage());
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub
                    Log.d("error",error.toString());
                }
            }
    ) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            String token = "",cookie="";
            token = getSharedPreferences(getResources().
                    getString(R.string.pref_login_data), Context.MODE_PRIVATE)
                    .getString("login_token","");
            cookie = "token="+token+";";
            Map<String, String>  param = new HashMap<String, String>();
            param.put("api-key", myAPIKey);
            param.put("Cookie", cookie);
            return param;
        }
    };
    Volley.newRequestQueue(this).add(getRequest);

每次调用 onErrorResponse 都会给出 AuthFailure 错误

但是当我在邮递员中点击具有相同标题的相同 url 时,它可以工作并返回 json 字符串 postman的截图如下:

我正在努力解决这个问题超过 3 天,任何帮助将不胜感激

【问题讨论】:

    标签: android android-volley


    【解决方案1】:

    仅将 Request.Method.GET 更改为 Request.Method.POST。

    因为你是通过POST方式发送参数,但是你使用了Get方式!

    【讨论】:

    • M.ghorbani 这是我想要的 GET 请求,而不是 POST 请求,我发送的是标头而不是参数。我认为参数是通过覆盖 getParams() 发送的
    猜你喜欢
    • 2016-05-04
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    相关资源
    最近更新 更多