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

问题描述 投票:0回答:1
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);

以上方法均无效,我总是得到空字符串,不知道为什么:(

enter image description here

您可以在图片中看到字节,错误响应实际上是这个:

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

我正在使用:

implementation 'com.android.volley:volley:1.1.1'
android android-volley android-webservice
1个回答
0
投票

您可以使用GSON将您的响应转换为json对象,

 val customPojo = CustomPojo()

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

CustomPojo应该是您的响应模型类。

© www.soinside.com 2019 - 2024. All rights reserved.