[英]Exception in server by android retrofit
I'm trying to throw an exception on my server so that my application on Android gets this exception and shows it, I inquired about this service at the postman, the answer I can get, was the message I send from the server "the code is not found in bd. " but in the application only the error "An error has occurred" is displayed. 我正在尝试在服务器上引发异常,以便我在Android上的应用程序获得此异常并显示该异常,我在邮递员处询问了该服务,得到的答案是我从服务器发送的消息“代码在bd中找不到“”,但在应用程序中仅显示错误“发生错误”。
this is the service and the throw: 这是服务和抛出:
internal TDDC InfoTD()
{
throw new Exception("the code is not found in bd");
}
this is the postman 这是邮递员
{
"Message": "An error has occurred.",
"ExceptionMessage": "the code is not found in bd",
"ExceptionType": "System.Exception",
And in android 并在android中
String errorResponse = response.errorBody().string();
JSONObject object = new JSONObject(errorResponse);
if (object.has("Message"))
message = String.valueOf(object.get("Message"));
¿ how i can get the message in "ExceptionMEssage" in android application ?, in this I use Retrofit 2 ¿我如何在android应用程序的“ ExceptionMEssage”中获取消息?在此我使用Retrofit 2
Its a bad practice to implemt or parse network error responses on your each service call. 在每个服务调用上实施或解析网络错误响应是一种不好的做法。 Instead you can centralize a catch or network error like this.
相反,您可以像这样集中捕获或网络错误。
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
okhttp3.Response response = chain.proceed(request);
// check your network responses here
if (response.code() == 500) {
startActivity(
new Intent(
ErrorHandlingActivity.this,
ServerIsBrokenActivity.class
)
);
return response;
}
return response;
}
})
.build();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.