2015-04-06 48 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>() 
相關問題