Mur*_*gat 11 java android interceptor retrofit okhttp
我正在使用OkHttp和Retrofit来制作我的应用程序的网络请求.我还使用拦截器进行身份验证,并在必要时重试请求.
服务器有时会出现临时问题,并返回空体,尽管响应状态为200 OK.这会导致我的应用程序崩溃,因为调用了Retrofit Callback的成功块,返回(并使用GSON解析)的自定义对象为null,成功回调中的代码假定返回一个对象.
我已经向服务器团队报告了这个问题,但我也想修复它,而不必使用null检查将所有成功回调代码包装在应用程序上.
Currenty我倾向于两个选项,虽然任何其他想法都是最受欢迎的:1)不从拦截器返回(这甚至可能吗?)并且只显示一个错误对话框2)返回一些会使Retrofit调用失败的部分打回来.
我的代码如下.正如您所看到的,当收到空体时,我会重试该请求最多3次.
@Override
public Response intercept(Chain chain) throws IOException
{
// First
Request request = chain.request();
Response response = chain.proceed(request);
....
....
....
// Retry empty body response requests for a maximum of 3 times
Integer retryMaxCount = 3;
MediaType contentType = response.body().contentType();
String bodyString = response.body().string();
while (bodyString.length() == 0 && retryMaxCount > 0)
{
//Empty body received!, Retrying...
retryMaxCount--;
response = chain.proceed(request);
bodyString = response.body().string();
}
if (bodyString.length() != 0)
{
// Create and return new response because it was consumed
ResponseBody newResponseBody = ResponseBody.create(contentType, bodyString);
return response.newBuilder().body(newResponseBody).build();
}
else
{
// WHAT TO WRITE HERE???
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢.
刚刚遇到了相同的情况,这篇文章帮助我实施了解决方案。感谢@mastov 指出正确的方向。
使用后端 api,即使出现错误也始终返回 HTTP 200。这是我的错误响应示例
{"status":403,"message":"Bad User credentials","time":1495597740061,"version":"1.0"}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的实现来补充这个答案。
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
ResponseBody body = response.body();
// Only intercept JSON type responses and ignore the rest.
if (body != null && body.contentType() != null && body.contentType().subtype() != null && body.contentType().subtype().toLowerCase().equals("json")) {
String errorMessage = "";
int errorCode = 200; // Assume default OK
try {
BufferedSource source = body.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
Charset charset = body.contentType().charset(Charset.forName("UTF-8"));
// Clone the existing buffer is they can only read once so we still want to pass the original one to the chain.
String json = buffer.clone().readString(charset);
JsonElement obj = new JsonParser().parse(json);
// Capture error code an message.
if (obj instanceof JsonObject && ((JsonObject) obj).has("status")) {
errorCode = ((JsonObject) obj).get("status").getAsInt();
}
if (obj instanceof JsonObject && ((JsonObject) obj).has("message")) {
errorMessage= ((JsonObject) obj).get("message").getAsString();
}
} catch (Exception e) {
Log.e(TAG, "Error: " + e.getMessage());
}
// Check if status has an error code then throw and exception so retrofit can trigger the onFailure callback method.
// Anything above 400 is treated as a server error.
if(errorCode > 399){
throw new Exception("Server error code: " + errorCode + " with error message: " + errorMessage);
}
}
return response;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9294 次 |
| 最近记录: |