package com.github.hunter0x7c7.sync.model.server.net.converters; import okhttp3.ResponseBody; import retrofit2.Converter; import retrofit2.Retrofit; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Type; /** * ================================================================ *

* 版 权: HunterHuang(c)2020 *

* 作 者: Hunter_1125607007@QQ.COM *

* 版 本: V1.0 *

* 创建日期: 2020/6/23 21:05 *

* 描 述:解决当接口请求的数据为空,返回没有响应体body,只有响应头header,content-length为0的Response,这时候GsonConverterFactory就解析异常 *

*

* 修订历史: *

* ================================================================ */ public class NullOnEmptyConverterFactory extends Converter.Factory { @Override public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { final Converter delegate = retrofit.nextResponseBodyConverter(this, type, annotations); return new Converter() { @Override public Object convert(ResponseBody body) throws IOException { if (body.contentLength() == 0) return null; return delegate.convert(body); } }; } }