Volley 错误输出服务器端发送的错误信息
Volley error output the error message sent from server side
我在 php 中使用 volley 并返回一个错误,其结构如 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";
}
};
我卡在如何从 volley 错误中读取错误消息。我如何使用或不使用 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")
使用
String errorResponse = new String(error.networkResponse.data);
然后将此字符串转换为您的 json 对象
尝试从您的 api 回复中获取 response.errorbody
我在 php 中使用 volley 并返回一个错误,其结构如 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";
}
};
我卡在如何从 volley 错误中读取错误消息。我如何使用或不使用 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")
使用
String errorResponse = new String(error.networkResponse.data);
然后将此字符串转换为您的 json 对象
尝试从您的 api 回复中获取 response.errorbody