解决 Retrofit response.body() on a null object reference

在使用Retrofit时,我们一般使用的是GsonConverterFactory转换器。但是有时候后台会返回为空的response。
app端会返回response.body() on a null object reference 错误。

  @GET("empty")
    Observable<Void> testEmpty();

解决方案:

@GET("empty")
    Observable<ResponseBody> testEmpty();

亲测有用。

 @Override
    public void viewDidCreated() {
        BaseHttpMethod.getInstance().create(BillService.class)
                .testEmpty()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(CommonSubscribe.newInstanceWithOutProgress(this, this::testEmptySucceed));
    }

    private void testEmptySucceed(ResponseBody response) {
        LogUtil.i("test", "success");
    }
I/okhttp: <-- 200 http://192.168.1.78:8080/empty-response/empty (43ms)
I/okhttp: Content-Type: text/plain;charset=UTF-8
I/okhttp: Content-Length: 0
I/okhttp: Date: Fri, 25 May 2018 07:08:53 GMT
I/okhttp: <-- END HTTP (0-byte body)
D/OpenGLRenderer: Enabling debug mode 0
I/test: success
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容