我从服务器空json(“{}”)获取代码为204的删除响应。
在(lldb) po searchBar
error: warning: couldn't get cmd pointer (substituting NULL): extracting data from value failed
Couldn't materialize: couldn't get the value of variable searchBar: variable not available
Errored out in Execute, couldn't PrepareToExecuteJITExpression
课程中,有一件令人烦恼的事情被抛出:
okhttp3.internal.http.HttpEngine
如果你尝试在标题中返回没有内容(服务器端)的内容,那么Content-Length大于0;
任何非服务器端的想法如何解决这个问题?
答案 0 :(得分:4)
您可以在拦截器中捕获ProtocolException
并返回占位符204 Response
。使用这种方法的注意事项 - 1)您最终可能会捕获其他协议错误(重定向太多等)。如果这是一个问题,您可以将e.getMessage()
与okhttp的异常消息进行比较,如果没有匹配则重新抛出异常。 2)您仍然无法访问原始回复,因此如果您需要检查任何返回的标题,那么运气不好。
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response;
try {
response = chain.proceed(chain.request());
} catch (ProtocolException e) {
response = new Response.Builder()
.request(chain.request())
.code(204)
.protocol(Protocol.HTTP_1_1)
.build();
}
return response;
}
});
答案 1 :(得分:2)
如果稍有不同,可以避免这种情况。代替:
@DELETE("vehicles/{id}/")
Observable<Response<BaseResponse>> deleteVehicle(@Header("Authorization") String token, @Path("id") Long vehicleId);
我使用:
@HTTP(method = "DELETE", path = "vehicles/{id}/", hasBody = true)
Observable<Response<BaseResponse>> deleteVehicle(@Header("Authorization") String token, @Path("id") Long vehicleId);