-2

I have a clean and simple question how to read this using Android Retrofit 2.0

  {
  "status": true,

 "body": {

    "expertises": {
      "1": "Family Law",
      "2": "Land and Asset",
      "3": "Sexual Offense"
    }
  }
}

I want to read "expertises"

I have used model classes as follows: Expertises:

 public class Expertises {
    @SerializedName("status")
    private String status;
    @SerializedName("expertises")
    ExpertisesResponse expertisesResponse;

    public ExpertisesResponse getExpertisesResponse() {
        return expertisesResponse;
    }

    public void setExpertisesResponse(ExpertisesResponse expertisesResponse) {
        this.expertisesResponse = expertisesResponse;
    }

    public String getStatus() {
        return status;
    }

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

ExpertisesResponse:

public class ExpertisesResponse {

    @SerializedName("1")
    private String expertiseOne;
    @SerializedName("2")
    private String expertiseTwo;
    @SerializedName("3")
    private String expertiseThree;

    public String getExpertiseOne() {
        return expertiseOne;
    }

    public ExpertisesResponse(String expertiseOne, String expertiseTwo, String   expertiseThree) {
        this.expertiseOne = expertiseOne;
        this.expertiseTwo = expertiseTwo;
        this.expertiseThree = expertiseThree;
    }



    public String getExpertiseTwo() {
        return expertiseTwo;
    }

    public String getExpertiseThree() {
        return expertiseThree;
    }

    public void setExpertiseOne(String expertiseOne) {
        this.expertiseOne = expertiseOne;
    }

    public void setExpertiseTwo(String expertiseTwo) {
        this.expertiseTwo = expertiseTwo;
    }

    public void setExpertiseThree(String expertiseThree) {
        this.expertiseThree = expertiseThree;
    }
}

But I am getting Exception NullPointer for ExpertisesResposne. PLease I want some improvement or correction if anything is wrong.

This is how I called :

 ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
        Call<Expertises> getExpertises = apiInterface.getExpertise();
        getExpertises.enqueue(new Callback<Expertises>() {
            @Override
            public void onResponse(Call<Expertises> call, Response<Expertises> response) {

                 Toast.makeText(getApplicationContext(),response.body().getExpertisesResponse().getExpertiseOne(),Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onFailure(Call<Expertises> call, Throwable t) {

            }
        });
    }
Danish
  • 51
  • 12

1 Answers1

0

You missed class Body covering class ExpertisesResponse. Should be like:

class Body {

@SerializedName("expertises")
    ExpertisesResponse expertisesResponse;

}

And class Expertises

public class Expertises {
    @SerializedName("status")
    private String status;
    @SerializedName("body")
    Body body;
}
RoShan Shan
  • 2,924
  • 1
  • 18
  • 38
  • Thank you it worked! Now one last thing is What if in future these "expertises" in json response increase? How to accommodate that scenario? – Danish Nov 30 '16 at 09:20
  • If values in `expertises` change. Have 2 ways to do: 1-restructure your json returned with unchnaged `key`. 2. Parse dynamic keys manually(you need to parse manually) [link] http://stackoverflow.com/questions/7304002/how-to-parse-a-dynamic-json-key-in-a-nested-json-result. I think if you use retrofit with auto parsing, you suggest you the first way. – RoShan Shan Nov 30 '16 at 09:23
  • OK Thanks RoShan Shan :) – Danish Nov 30 '16 at 09:29