2

i have this fragment of a json response from an endpoint in where i need to get some news, but is kinda returning me wrong info or maybe i'm doing this wrong:

{
    "data": [
        {
            "alignImages": "left",
            "data": [
                {
                    "content": "Despus de dos aos de la operacin y una intensiva terapia, jvenes que no podan mover sus miembros inferiores y superiores ahora pueden comer, cepi",
                    "id": 179,
                    "title": "Ciruga que reconecta los nervios le devolvi a 13 tetrapljicos la movilidad en",
                    "url_detail": "https://www.elespectador.com/noticias/salud/cirugia-que-reconecta-los-nervios-le-devolvio-13-tetraplejicos-la-movilidad-en-brazos-y-codos-articulo-869",
                    "url_image": "https://storage.googleapis.com/sitidoctor-161813.appspot.com/images/noticia_2.png"
                },
                {
                    ...
                }
            ],
            "imgHeader": "https://storage.googleapis.com/sitidoctor-161813.appspot.com/images/Noticias_Salud.png",
            "titleHeader": "Noticias Salud"
        }
    ],
    "message": "success!",
    "status": 200
}

This is the model i use to parse the response from the retrofit instance query:

public class NewsResponse {

    @SerializedName("data")
    @Expose
    private List<New> data = null;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("status")
    @Expose
    private Integer status;

    public List<New> getData() {
        return data;
    }

    public void setData(List<New> data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getStatus() {
        return status;
    }

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

}

and i make the request using retrofit like this:

private void handleResponse(NewsResponse newsResponse) {
        Log.e("response", newsResponse.getData().toString());
        if (newsResponse.getData().size() > 0){
            List<New> n = new ArrayList<>();
            for (int i = 0; i < newsResponse.getData().size(); i++){
                New nn = newsResponse.getData().get(i);
                n.add(nn);
            }
            callback.onSuccess(n);
        }
}

but i'm always getting the a single and empty value, is like is returning the inner data array as object but cant get data, i tried everything and got nothing, any ideas?

Reference imagen in debugger

4
  • Looking into your debugger image, Your newsResponse.getData().size() is 1. The only element in the list has id,title,content, uridetail as null. You are handling the response correctly. There is something wrong with your response. Please check if you are making the network call correctly Commented Mar 21, 2020 at 1:37
  • 1
    You're right the problem it was in the api response, due it was giving me a data[] array instead a data{} object at the top level, thanks <3 Commented Mar 24, 2020 at 22:27
  • I will put it in the answer, please accept it then. Commented Mar 24, 2020 at 22:32
  • Now you can upvote as well :) Commented Mar 24, 2020 at 22:45

1 Answer 1

1

Looking into your debugger image, Your newsResponse.getData().size() is 1. The only element in the list has id,title,content, uridetail as null. You are handling the response correctly. There is something wrong with your response. Please check if you are making the network call correctly

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.