Retrofit2 OkHttp3响应正文为空错误

标签 retrofit retrofit2 okhttp

我正在使用 Retrofit 2 调用微服务,该微服务在 PUT 方法上返回 200 和空响应正文。 然而,Retrofit 2 似乎无法处理这个问题,并且在“enqueue”中转到 onFailure 分支
@覆盖 public void onFailure(Call call, Throwable t) {

以下是日志:

Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log
INFORMATION: --> PUT http://127.0.0.1/test/ http/1.1
Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log
INFORMATION: Content-Type: application/vnd.tipico.notification-v1+json
Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log
INFORMATION: Content-Length: 87
Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log
INFORMATION: 
Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log
INFORMATION: {"state":"ACTIVE","externalId":"abcd","loginName":"gsdfgsdf","updatedAt":1495531062000}
Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log
INFORMATION: --> END PUT (87-byte body)
Mai 27, 2017 3:26:50 PM okhttp3.internal.platform.Platform log
INFORMATION: <-- 200  http://127.0.0.1/test/ (197ms)
Mai 27, 2017 3:26:50 PM okhttp3.internal.platform.Platform log
INFORMATION: X-Application-Context: customer-care-notification-service:49980
Mai 27, 2017 3:26:50 PM okhttp3.internal.platform.Platform log
INFORMATION: Content-Length: 0
Mai 27, 2017 3:26:50 PM okhttp3.internal.platform.Platform log
INFORMATION: Date: Sat, 27 May 2017 13:26:49 GMT
Mai 27, 2017 3:26:50 PM okhttp3.internal.platform.Platform log
INFORMATION: <-- END HTTP (0-byte body)
15:26:50,030 ERROR com.test.app.Test - Failed CCNS call com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: okhttp3.ResponseBody$BomAwareReader@9eb187b; line: 1, column: 0]

谁知道这是什么原因造成的?由于请求已成功处理(见上文)。

最佳答案

我为此问题创建了一个 Null 处理程序转换器:

public class NullOnEmptyConverterFactory extends Converter.Factory {

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        final Converter<ResponseBody, ?> delegate = retrofit.nextResponseBodyConverter(this, type, annotations);
        return (Converter<ResponseBody, Object>) body -> {
            if (body.contentLength() == 0) return null;
            return delegate.convert(body);
        };
    }
}

您需要在 Gson 转换器之前添加此转换器

.addConverterFactory(new NullOnEmptyConverterFactory())

关于Retrofit2 OkHttp3响应正文为空错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44217447/

相关文章:

android - 改造:@Field 和 @Body 有什么不同

android - 在改造 2 的 onResponse 中访问发送的参数

json - 我正面临这个错误 A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution

java - 在 android 中调用 API 并使用 Retrofit 2 收到此错误 :Response{protocol=h2, code=200, message=, url=https ://api. com/video/list-video.php}

java - OkHttp 缓存如何工作?

android - HTTP FAILED : java. io.IOException: 发出 https 请求时流异常的意外结束

Android Retrofit 2.0.0-beta2 Post string body 动态 header

flutter - 如何在 flutter 中使用 retrofit 在 dio api 调用中设置 responseType?

android - 如何等待 Okhttp 调用的结果以在测试中使用它?

android - 为什么我不能在 Retrofit 中向拦截器请求 OkHttp 添加 header ?