2015-04-06 50 views
3

Iam在android中调用web服务。在这我要拨打的URL我没有发送任何PARAMS到服务器,只需调用URL,BasicNetwork.performRequest:意外的响应代码401 Android Volley库

但它越来越像错误[10520] BasicNetwork.performRequest:意外的响应代码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); 

如何解决这个问题?

回答

1

HTTP 401意味着该网站需要身份验证,但未提供或未提供该身份验证。你需要认证自己。未知您是否需要提供HTTP基本认证,或者Web服务是否需要特殊认证,并且只是以其返回值而言很聪明。

+0

我打电话登录Web服务与JSON PARAMS其工作正常后,登录Web服务调用其不工作。任何猜测? – John 2015-04-06 09:45:17

+0

我是否需要将曲奇添加到排球头? – John 2015-04-06 09:46:04

+0

是的,这些图像需要身份验证 – 2015-04-06 09:47:03

0

此错误表示您需要进行身份验证。你可以这样做增加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); 
1

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

所以,更改GET在这一行发布

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