使用JSend格式使用Retrofit将JSON响应转换为POJO吗? [英] Converting JSON response to POJO with Retrofit using JSend format?

查看:97
本文介绍了使用JSend格式使用Retrofit将JSON响应转换为POJO吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用使用 JSend 格式的API.

I have to work with an API which using JSend format.

长话短说,它使用HTTP状态代码来表示状态,例如:

Long story short it is using HTTP status codes which indicates status like:

  • 200是成功
  • 406未经授权

这很好,因为我可以据此确定我的API请求是否成功.

Which is good because I can determine from this whether my API request is succeed or not.

但是:

由于JSend格式是它自己的东西,因此它在响应中还带有一些状态指示器,如下所示:

As JSend format has it's own thing, it has ALSO have a little status indicator at response just like this:

{
    status : "success",
    data : { "post" : { "id" : 2, "title" : "Another blog post", "body" : "More content" }}
}

因此它有一个状态"字段,该字段还显示API请求是否成功.

So it has a 'status' field which ALSO shows whether the API request is succeed or not.

问题:

进行改造以解析对POJO的响应,因此它假定响应仅包含模型,并且不包含成功指示符,例如:(一个后模型实例)

Retrofit made to parse the response to POJO so it assumes that the responses contains ONLY the Model and no indicators for success, just like this for example: (A post Model instance)

{ "id" : 2, "title" : "Another blog post", "body" : "More content" }

我的问题是:

有解决方案吗? 我是否可以预先准备状态指示器,拆分响应的数据"(模型)部分,然后进行改造以仅分析该部分?

Is there a solution for this? Can I pre-parse the status indicators, split the 'data' (Model) part of the response and give it to retrofit for parse only that part?

如果没有,我将不得不向每个模型添加状态"属性,这显然不是一种可行的方法,我不会那样做.

If not I would have to add a "status" attribute to each of my models which is clearly not a walkable way, I won't do that.

我应该坚持手动解析并使用ResponseBody而不是

Should I just stick with manual parsing and use ResponseBody instead of my Models at

void onResponse(Call<T> call, Response<T> response);是否表示T类型的参数?

void onResponse(Call<T> call, Response<T> response); for T type paramter?

因为这样,我可以使用.string()并将字符串转换为JSON,然后我可以手动解析模型,就像为它们编写解析器一样.

Because in that way I can use .string() and convert the string to JSON and after that I can parse my Models manually like writing the parser for them.

我真的很想使用Retrofit的功能来进行自动解析,因为使用 JSend 我只是不能想象一下,如果有的话,如何正确地做到这一点.

I would really like to use Retrofit's feature for automatic parsing because with JSend I just cannot imagine how could be this properly done if anyhow at all.

我无法通过这种方式更改API.

I cannot change the API it's going to be this way.

推荐答案

这是我的Response类.

public class Response<T> implements Serializable {
private T data;
private String status;

public T getData() {
    return data;
}

public void setData(T data) {
    this.data = data;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

}

这是我的api调用.

Callback<com.shippn.api.models.Response<T>> callback = new Callback<com.shippn.api.models.Response<T>>() {
        @Override
        public void onResponse(Call<com.shippn.api.models.Response<T>> call, Response<com.shippn.api.models.Response<T>> response) {
            checkStatus(response.body());
        }

        @Override
        public void onFailure(Call<com.shippn.api.models.Response<T>> call, Throwable t) {
            fail();
        }
    };
    call.enqueue(callback);

T参数是来自api的数据类型.我的意思是,如果您想获取Post类型的数据,则将Post插入到T中.如果要获取ArrayList<Post>,请放ArrayList<Post>.

T parameter is type of your data from api. I mean if you want to get data in Post type put Post insted of T. If you want to get ArrayList<Post> put ArrayList<Post>.

如果您的api响应中的data字段为空或为空,则Response.data为空或为空,则不会引发异常.

If data field is empty or null in your api response, Response.data will be null or empty, it wont throw an exception.

从api获得响应时,首先检查Response.status是否成功.如果是成功",则从Response获取数据,如果不执行错误操作.

When you get response from api, first check Response.status if it is "success" or not. If it is "success" get data from Response, if it is not take your error actions.

这篇关于使用JSend格式使用Retrofit将JSON响应转换为POJO吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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