Android 使用用户名和密码的 Volley GET 请求失败

Android Volley GET Request with username and password fails

以下 Android Volley GET 请求失败并显示以下错误消息:

E/Volley: [1038] BasicNetwork.performRequest: Unexpected response code 401 for 

这是请求:

    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    final String url = "https://xxxxxxxx/storage/v1/items";

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                StringBuilder formattedResult = new StringBuilder();
                JSONArray responseJSONArray = response.getJSONArray("items");
                for (int i = 0; i < responseJSONArray.length(); i++) {
                    formattedResult.append("\n" + responseJSONArray.getJSONObject(i).get("no") + "=> \t" + responseJSONArray.getJSONObject(i).get("ean")
                            + "=> \t" + responseJSONArray.getJSONObject(i).get("name")
                            + "=> \t" + responseJSONArray.getJSONObject(i).get("type")
                            + "=> \t" + responseJSONArray.getJSONObject(i).get("destruction")
                            + "=> \t" + responseJSONArray.getJSONObject(i).get("archived")
                    );
                }
                txtShowTextResult.setText("List of Items \n" + " Number" + "\t EAN \n" +
                        "\t Name \n" +  "\t Type \n" + formattedResult);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            txtShowTextResult.setText("An Error occured while making the request");
        }
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            String credentials = "username:password";
            String auth = "Basic "
                    + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
            headers.put("Content-Type", "application/json");
            headers.put("Authorization", auth);
            return headers;
        }
    });

    requestQueue.add(jsonObjectRequest);

我不确定请求失败的原因,因为我是 Android Volley 的新手。

可能的原因:没有调用 getHeaders() 方法,但我不知道将方法调用放在请求中的什么位置,因此,我将不胜感激任何帮助、提示或反馈。提前致谢!

在您的代码中添加缺失的部分,您可以尝试以下操作。请注意 getHeadersoverride 部分,告诉我进展如何。

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        //handle response
                    }
                }
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //handle error
            }
        }){
            @NotNull
            @Override
            public Map<String, String> getHeaders() {
                //Handle headers inside here
                Map<String, String> headers = new HashMap<>();
                String credentials = "username:password";
                String auth = "Basic "
                        + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                headers.put("Content-Type", "application/json");
                headers.put("Authorization", auth);
                return headers;
            }
        };

        requestQueue.add(jsonObjectRequest);