BasicNetwork.performRequest:意外响应代码401 android Volley库

时间:2015-04-06 09:39:00

标签: android http-headers android-volley

  

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

但它的错误如[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);

如何解决这个问题?

6 个答案:

答案 0 :(得分:3)

此错误表示您需要进行身份验证。您可以这样做,将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 :(得分:1)

HTTP 401表示网站需要身份验证,但未提供或失败。您需要对自己进行身份验证。未知您是否需要提供HTTP基本身份验证,或者Web服务是否需要特殊身份验证,并且只是对其返回值很聪明。

答案 2 :(得分:1)

如果我们使用 POST代替GET GET代替POST ,则会发生此错误

所以,在这一行中将GET改为发布

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

答案 3 :(得分:0)

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;
}

答案 4 :(得分:0)

添加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 :(得分:0)

添加标题...也许您忘记了在排球请求中添加标题。