zbryan
zbryan

Reputation: 875

Retrofit 2 returns null on its member variables

I'm currently studying retrofit 2 and came across with a problem.

I'm using a free api

http://services.groupkt.com/country/get/all

Here are my POJOs

RestResponse

public class RestResponse {

    @SerializedName("messages")
    @Expose
    private List<String> messages;
    @SerializedName("result")
    @Expose
    private List<Result> result;

    public List<String> getMessages() {
        return messages;
    }

    public void setMessages(List<String> messages) {
        this.messages = messages;
    }

    public List<Result> getResult() {
        return result;
    }

    public void setResult(List<Result> result) {
        this.result = result;
    }

}

Result

public class Result {

    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("alpha2_code")
    @Expose
    private String alpha2Code;
    @SerializedName("alpha3_code")
    @Expose
    private String alpha3Code;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAlpha2Code() {
        return alpha2Code;
    }

    public void setAlpha2Code(String alpha2Code) {
        this.alpha2Code = alpha2Code;
    }

    public String getAlpha3Code() {
        return alpha3Code;
    }

    public void setAlpha3Code(String alpha3Code) {
        this.alpha3Code = alpha3Code;
    }
}

ApiInterface

public interface ApiInterface {

    @GET("country/get/all")
    Call<RestResponse> getCountry();

}

MainActivity

ApiInterface apiService = ApiClient.getmRetrofitClient().create(ApiInterface.class);
        Call<RestResponse> call = apiService.getCountry();
        call.enqueue(new Callback<RestResponse>() {
            @Override
            public void onResponse(Call<RestResponse> call, Response<RestResponse> response) {
                List<Result> results = response.body().getResult();
            }

            @Override
            public void onFailure(Call<RestResponse> call, Throwable t) {
                Log.e("onFailure", t.getMessage());
            }
        });

The retrofit calls the onResponse callback, but when I'm trying to get the result from the response.body() it returns null. How can I fix this?

Upvotes: 0

Views: 650

Answers (1)

manman
manman

Reputation: 5113

So you would need a different class also for holding the RestResponse, because the Json response that you're getting has first level object RestResponse and then that one is the class that you have.

enter image description here

So you would need something like:

public class CountryResponse {

    @SerializedName("RestResponse")
    @Expose
    private RestResponse restResponse;

    public RestResponst getRestResponse() { return restResponse; }
    public void setRestResponse(RestResponse restResp) {restResponse = restResp; }
}

And you just need to change the API call line to:

ApiInterface apiService = ApiClient.getmRetrofitClient().create(ApiInterface.class);
    Call< CountryResponse > call = apiService.getCountry();
    call.enqueue(new Callback< CountryResponse >() {
        @Override
        public void onResponse(Call<CountryResponse> call, Response<CountryResponse> response) {
            List<Result> results = response.body().getRestResponse().getResult();
        }

        @Override
        public void onFailure(Call< CountryResponse > call, Throwable t) {
            Log.e("onFailure", t.getMessage());
        }
});

Try this and this should work.

Upvotes: 1

Related Questions