解决 Retrofit response.body() on a

2018-05-25  本文已影响0人  blossom_6694

在使用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
上一篇 下一篇

猜你喜欢

热点阅读