簡體   English   中英

使用 Volley 使用 StringRequest 時出現 AuthFailure 錯誤

[英]Getting AuthFailure Error when using StringRequest using Volley

我按如下方式發送 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 錯誤

但是當我在郵遞員中使用相同的標題點擊相同的網址時,它會起作用並且返回json字符串,郵遞員的屏幕截圖如下:

郵遞員回復

我正在努力解決這個問題超過 3 天,任何幫助將不勝感激

僅將 Request.Method.GET 更改為 Request.Method.POST。

因為你是通過POST方式發送參數,但是你使用了Get方式!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM