【问题标题】:BasicNetwork.performRequest: Unexpected response code 401 android Volley libraryBasicNetwork.performRequest:意外的响应代码 401 android Volley 库
【发布时间】:2015-04-06 09:39:00
【问题描述】:

我正在调用 android 中的 Web 服务。因为我想调用 URL 我没有向服务器发送任何参数,只是调用 URL,

但它会出现类似 [10520] BasicNetwork.performRequest: Unexpected response code 401 的错误

我的代码是

RequestQueue queue = Volley.newRequestQueue(getActivity()); 
    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, Server.URL, null,
            new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response) {  
                                // display response    
                    hideProgressDialog();

                }
            },
            new Response.ErrorListener()
            {
                 @Override
                 public void onErrorResponse(VolleyError error) {           
                     hideProgressDialog();
               }
            }
        );

        // add it to the RequestQueue  
        queue.add(getRequest);

如何解决?

【问题讨论】:

    标签: android http-headers android-volley


    【解决方案1】:

    此错误表示您需要进行身份验证。您可以将 getHeaders() 添加到您的代码中,如下所示:

    RequestQueue queue = Volley.newRequestQueue(getActivity()); 
    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, Server.URL, null,
            new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response) {  
                    // display response    
                    hideProgressDialog();
    
                }
            },
            new Response.ErrorListener()
            {
                 @Override
                 public void onErrorResponse(VolleyError error) {           
                     hideProgressDialog();
               }
            }
    
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/json");
                String creds = String.format("%s:%s","username","password");
                String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
                params.put("Authorization", auth);
                return params;
            }
        );
    
    queue.add(getRequest);
    

    【讨论】:

      【解决方案2】:

      HTTP 401 表示网站需要身份验证,但未提供或失败。您需要对自己进行身份验证。不知道您是否需要提供 HTTP 基本身份验证,或者 Web 服务是否需要特殊身份验证并且只是巧妙地处理了它的返回值。

      【讨论】:

      • 我正在使用 json 参数调用登录 Web 服务,在登录 Web 服务调用它不工作后它工作正常。任何猜测???
      • 是的,这些图片需要认证
      【解决方案3】:

      如果我们使用 POST 而不是 GETGET 而不是 POST,则会出现此错误

      所以,将 GET 更改为在此行中发布

      JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, Server.URL, null, new Response.Listener<JSONObject>()
      

      【讨论】:

        【解决方案4】:

        添加getHeader

        @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("token", SharedVariables.TOKEN);
                    headers.put("device", SharedVariables.DEVICE_ID);
                    headers.put("accept-language", "en-US");
                    headers.put("api-version", "1.0");
                    return headers;
                }
        

        【讨论】:

          【解决方案5】:

          添加标头...也许您忘记在 volley 请求中添加标头。

          【讨论】:

            【解决方案6】:
            String http_post() {
            
            RequestQueue MyRequestQueue = Volley.newRequestQueue(Library.this);
            
            //String url = "http://" + "hqplayer" + ":" + Utils.password + "@" + Utils.ip + ":8088/library";
            String url = "http://" + Utils.ip + ":8088/library";
            Log.i(TAG, "HttpPost() <" + url + ">");
            String credentials = "hqplayer:valvole";
            byte[] t = credentials.getBytes();
            byte[] auth = Base64.encode(t, Base64.DEFAULT);
            final String basicAuthValue = new String(auth);
            MyRequestQueue.add(new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
               @Override
               public void onResponse(String response) {
                   Log.i(TAG, "HttpPost() - onResponse() ");               
               }
            }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   Log.i(TAG, "HttpPost() - onErrorResponse() ");
                   Log.i(TAG, "HttpPost() error <" + error + ">");
               }
            }) {
               @Override
               public Map<String, String> getHeaders() throws AuthFailureError {
                   HashMap<String, String> params = new HashMap<String, String>();
                   String creds = String.format("%s:%s","hqplayer","valvole");
                   String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
                   params.put("Authorization", auth);
                   return params;
                }});
               return null;
            }
            

            【讨论】:

            • 请在您的答案中添加一些上下文。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-01-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-06
            • 1970-01-01
            相关资源
            最近更新 更多