Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

android - Converting JSON response to POJO with Retrofit using JSend format?

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

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

  • 200 is Success
  • 406 is Unauthorized

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

BUT:

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" }}
}

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

PROBLEM:

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" }

My question is:

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.

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

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

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.

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is my Response class.

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;
}

}

Here is my api call.

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 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>.

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...