0

how to access districtID(key) value and name(key) value using retrofit ?

this is json file...

{
    "error": false,
    "message": "District successfully fetched.",
    "districts": [
        {
            "districtID": "DIS260920181",
            "name": "Raipur"
        },
        {
            "districtID": "DIS260920182",
            "name": "Bilaspur"
        },
        {
            "districtID": "DIS011020186",
            "name": "korba"
        },
        {
            "districtID": "DIS011020187",
            "name": "jagdalpur"
        },
        {
            "districtID": "DIS021020188",
            "name": "surguja"
        },
        {
            "districtID": "DIS021020189",
            "name": "Mungeli"
        }
    ]
}

Please help :(

AskNilesh
  • 67,701
  • 16
  • 123
  • 163

3 Answers3

0

Convert JSON Array into POJO classes and then call api using retrofit network call.

  • after this i'm facing problem - **java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $** – Surjeet Nandi Oct 31 '18 at 08:55
0

Use this link

copy your json and it gives you two model class you should put the parent class which contains error , message and districts as your output in your request. something like this

@GET("your_url")
Call<ModelExample> nameOfMethod();

And then you have list of districts. you can access to the items and districtID and name I hope it helps you :)

amirhesni
  • 441
  • 1
  • 6
  • 22
0
      for onResponse:
   --------------------------
        Call<ModalClass> daoCall = api.getconnectdata();
          modelclass.enqueue(new Callback<ModalClass>() {
           @Override
            public void onResponse(Call<ModalClass> call, Response<ModalClass> 
            response) {
                if(response.isSuccessful()){
                    modelclass= response.body();
             List<Districts>  list = modelclass.getAddData().getData();
             adapter = new CustomListAdapter(getApplicationContext(),list);
               listView.setAdapter(adapter);
                }
            }