尝试使用Android Volley执行JsonObjectRequest的客户端错误

时间:2019-05-05 01:01:31

标签: android android-volley

我正在尝试使用JSON对需要HTTP Basic Auth的服务进行HTTP PUT,但是我一直收到客户端错误。我这样做:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.PUT, payOp.getURL(), payOp.getJson(), new Response.Listener<JSONObject>() {
         @Override
         public void onResponse(JSONObject response) {
             Log.i("jsonObjectRequest", "Response " + response.toString());
         }
     }, new Response.ErrorListener() {
         @Override
         public void onErrorResponse(VolleyError error) {
             Log.i("jsonObjectRequest", "Error " + error.toString());
             Log.i("jsonObjectRequest", "Error ", error);
         }
     }){
         @Override
         public Map<String, String> getHeaders() throws AuthFailureError {
             HashMap<String, String> params = new HashMap<String, String>();

             ApplicationSettings applicationSettings = ApplicationSettings.getInstance();
             String username = applicationSettings.mMID;
             String password = applicationSettings.mAPIPassword;

             String creds = String.format("%s:%s",username, password);
             String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP);
             Log.i("jsonObjectRequest","Authorization is: " + auth);
             params.put("Authorization", auth);
             return params;
         }
     };

     queue.add(jsonObjectRequest);

然后我回来:

2019-05-04 17:52:40.158 7430-7430/com.amex.gatewaydemotr5 I/jsonObjectRequest: Error 
    com.android.volley.ClientError
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:199)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)

我不知道自己在做什么错。我正在使creds字符串正确(我有另一个用node.js编写的应用程序,可以将输出与之进行比较),但是我并没有从Volley收到很多错误输出。谁能看到我在做什么错?有什么办法可以从Volley上获取有关该错误的更多详细信息?谢谢!

1 个答案:

答案 0 :(得分:0)

好,知道了。这是我的URL和数据存在的问题,但是最主要的是我不了解如何解析错误,因为任何需要知道错误的人都有一个具有statusCode和data参数的networkResponse对象。服务器消息在那里。 data参数以字节数组形式返回,因此必须将其包装在字符串对象中才能查看结果。

一旦我知道我可以找回所需的消息并解决问题,则代码如下:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.PUT, payOp.getURL(), payOp.getJson(), new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        Log.i("jsonObjectRequest", "Response " + response.toString());
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.i("jsonObjectRequest", "Error, Status Code " + error.networkResponse.statusCode);
        Log.i("jsonObjectRequest", "URL: " + payOp.getURL());
        Log.i("jsonObjectRequest", "Payload: " + payOp.getJson().toString());
        Log.i("jsonObjectRequest", "Net Response to String: " + error.networkResponse.toString());
        Log.i("jsonObjectRequest", "Error bytes: " + new String(error.networkResponse.data));

    }
}){
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> params = new HashMap<String, String>();

        ApplicationSettings applicationSettings = ApplicationSettings.getInstance();
        String username = applicationSettings.mMID;
        String password = applicationSettings.mAPIPassword;

        String creds = String.format("%s:%s",username, password);
        String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP);
        Log.i("jsonObjectRequest","Authorization is: " + auth);
        params.put("Authorization", auth);
        return params;
    }
};

重要的是:

new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
    Log.i("jsonObjectRequest", "Error, Status Code " + error.networkResponse.statusCode);
    Log.i("jsonObjectRequest", "URL: " + payOp.getURL());
    Log.i("jsonObjectRequest", "Payload: " + payOp.getJson().toString());
    Log.i("jsonObjectRequest", "Net Response to String: " + error.networkResponse.toString());
    Log.i("jsonObjectRequest", "Error bytes: " + new String(error.networkResponse.data));

}