如何处理空响应体改造2? [英] How can I handle empty response body with Retrofit 2?

查看:219
本文介绍了如何处理空响应体改造2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我开始使用改造2和我面临的问题与解析空响应体。我只用HTTP code响应没有响应体内的任何内容的服务器。
我如何处理有关服务器的响应(头,状态code等)?

Recently I started using Retrofit 2 and I faced an issue with parsing empty response body. I have a server which responds only with http code without any content inside the response body. How can I handle only meta information about server response (headers, status code etc)?

在此先感谢!

推荐答案

编辑:

由于杰克沃顿指出,

@GET("/path/to/get")
Call<Void> getMyData(/* your args here */);

是去与我最初的反应的最好方法 -

is the best way to go versus my original response --

您可以只返回一个 ResponseBody ,这将绕过解析响应。

You can just return a ResponseBody, which will bypass parsing the response.

@GET("/path/to/get")
Call<ResponseBody> getMyData(/* your args here */);

然后在你的电话,

Then in your call,

Call<ResponseBody> dataCall = myApi.getMyData();
dataCall.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Response<ResponseBody> response) {
        // use response.code, response.headers, etc.
    }

    @Override
    public void onFailure(Throwable t) {
        // handle failure
    }
});

这篇关于如何处理空响应体改造2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆