我在php中使用截击并返回一个错误,其结构如下postman中所示
{
"name": "Exception",
"message": "Unable to verify your accont",
"code": 500,
"type": "yii\\base\\UserException"
}
现在我想阅读上面的错误信息,所以在我的截击字符串请求中
StringRequest stringRequest = new StringRequest(this.request_method, this.api_url,
response -> {
},
error -> {
//here process the error
if (error instanceof ServerError || error instanceof AuthFailureError){
NetworkResponse response = er.networkResponse;
switch (response.statusCode){
case 500:{
HashMap<String, String> result = new Gson().fromJson(....); //stuck
}
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
我一直纠结于如何从截击错误中读取错误信息。我如何继续使用或不使用GSON
我仍然希望使用字符串请求,因为我希望php的响应是json编码的字符串。
最佳答案
尝试用这种方式从NetworkResponse
读取响应
科特林
val response = er.networkResponse
val jsonError = String(response.data)
val responseObject = JSONObject(response)
val message = responseObject.optString("message")
对于Java
NetworkResponse response = er.networkResponse;
String responseData = new String(response.data,"UTF-8");
JSONObject responseObject =new JSONObject(response)
String message = responseObject.optString("message")