Volley 错误响应字节不会从字节转换为字符串

Volley error response bytes does not convert from bytes to string

CustomStringRequest customStringRequest = new CustomStringRequest(requestMethod.ordinal(), serverUrl,
            result -> {
                Log.d(TAG, "headers: " + result.headers);
                Log.d(TAG, "response: " + result.response);
                createTaskItem(result);
            },
            error -> {
                Log.d(TAG, "error: " + error);
            })

错误在 JSON 字符串中,我试图得到这样的错误:

new String(error.networkResponse.data, "utf-8");
new String(error.networkResponse.data);

none 上述方法有效,我总是得到空字符串,不知道为什么:(

如图所示,错误响应实际上是这样的:

   {
  "error": [
    "Wrong Credentials!"
  ],
  "email_exists": false
}

我正在使用:

implementation 'com.android.volley:volley:1.1.1'

您可以使用 GSON ,

将您的响应转换为 json 对象
 val customPojo = CustomPojo()

 val gson = Gson()
 gson.toJson(myPojo)

CustomPojo 应该是您的响应模型 class.

哦,我只是在评估调试模式下的错误响应 new String(error.networkResponse.data, "UTF-8"); 结果是 returns 空字符串。 我只是尝试打印错误响应并且成功了!。谢谢