0

i am using retrofit with Gson GsonConverterFactory for my API connection. i have trouble to fetch my API.

{
    status: 100,
    tb_name: "list",
    data: {
        1: {
            distance: "",
            Busid: "18",
            locid: "6309",
            business_name: " Park",
            category_name: "Activities",
            area: "Sharjah",
            cat_image: "14645915394584643.png",
            cusine: "",
            city: "Sharjah",
            lat_long: "25.342403, 55.379542",
            image: "14650439123004854.jpg",
            is_fav: "No"
        },
        2: {
            distance: "",
            Busid: "56",
            locid: "6311",
            business_name: "Bowling City",
            category_name: "Activities",
            area: "Abu Dhabi",
            cat_image: "14645915394584643.png",
            cusine: "",
            city: "Abu Dhabi",
            lat_long: "24.494183, 54.367608",
            image: "14650440703886102.jpg",
            is_fav: "No"
        },
        3: {
            distance: "",
            Busid: "56",
            locid: "6312",
            business_name: "Bowling City",
            category_name: "Activities",
            area: "Deira, Dubai",
            cat_image: "14645915394584643.png",
            cusine: "",
            city: "Dubai",
            lat_long: "25.251964, 55.332805",
            image: "14650440703886102.jpg",
            is_fav: "No"
        },
        4: {
            distance: "",
            Busid: "56",
            locid: "6310",
            business_name: "Bowling City",
            category_name: "Activities & Leisure",
            area: "Al Ain Mall, Al Ain",
            cat_image: "14645915394584643.png",
            cusine: "",
            city: "Al Ain",
            lat_long: "24.222321, 55.782205",
            image: "14650440703886102.jpg",
            is_fav: "No"
        }

    }
}

i created pojo class

public class ktbresponse {
    private String status;
    private String tb_name;

    public String getStatus() {
        return status;
    }

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

    public String getTb_name() {
        return tb_name;
    }

    public void setTb_name(String tb_name) {
        this.tb_name = tb_name;
    }

    public application.myrestapi.com.myrestapiapplication.model.data getData() {
        return data;
    }

    public void setData(application.myrestapi.com.myrestapiapplication.model.data data) {
        this.data = data;
    }

    private data data;
}


public class data {
    private String distance;
    private String Busid;

    public String getDistance() {
        return distance;
    }

    public void setDistance(String distance) {
        this.distance = distance;
    }

    public String getBusid() {
        return Busid;
    }

    public void setBusid(String busid) {
        Busid = busid;
    }

    public String getLocid() {
        return locid;
    }

    public void setLocid(String locid) {
        this.locid = locid;
    }

    public String getBusiness_name() {
        return business_name;
    }

    public void setBusiness_name(String business_name) {
        this.business_name = business_name;
    }

    private String locid;
    private String business_name;
}

But when i try to print

Log.e("apiResponse",apiResponse.getStatus()); Log.e("apiResponse",apiResponse.getData().getBusiness_name());

only print status.not working data. i am first time seeing this type of structure.Let me know what i did wrong. How to create Pojo class for this structure?

mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
praj
  • 849
  • 1
  • 16
  • 39
  • You have to use List data not just simple Data object in your `ktbresponse` class. – parohy Aug 08 '16 at 08:06
  • @parohy java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 40 path $.data – praj Aug 08 '16 at 08:12
  • 1
    your json means you have an object `data`, it has objects `1 ,2 and 3` , i think it may get more objects 5 , 6 ... at the same time it's not an array, which is not the proper way to do it, because in the POJO you will have to have attributes (properties) with names 1 2 3 which is not valid (variable names can't start with numbers) so what i suggest is re-create a new json that will have data[] array, and this way you will be able to use `List data;` in your POJO, as of now, if you use `List` you will get the exception `Expected BEGIN_ARRAY but was BEGIN_OBJECT...` – Yazan Aug 08 '16 at 08:41
  • How does your rest look like? How did you define the interface? In json do just `"data" : {object}, {object}...` delete those numbers – parohy Aug 08 '16 at 08:41
  • P.S: my comment above is in respect of using `GSON` (auto parse) but if you want to manually parse the json response i.e: `jsonObj.getJsonObject("1");` and append objects (1,2,3...) into an array of type ` Data` it's perfectly valid. – Yazan Aug 08 '16 at 08:50
  • @parohy its not possible to modify.its done long before and done by some one – praj Aug 08 '16 at 09:11

2 Answers2

2

Are you able to modify the API? If yes, just change the "data" object to be an Array instead of an object with fields "1", "2", "3". The Json would look like:

{
status: 100,
tb_name: "list",
data: [
    {
        distance: "",
        Busid: "18",
        locid: "6309",
        business_name: " Park",
        category_name: "Activities",
        area: "Sharjah",
        cat_image: "14645915394584643.png",
        cusine: "",
        city: "Sharjah",
        lat_long: "25.342403, 55.379542",
        image: "14650439123004854.jpg",
        is_fav: "No"
    },
    {
        distance: "",
        Busid: "56",
        locid: "6311",
        business_name: "Bowling City",
        category_name: "Activities",
        area: "Abu Dhabi",
        cat_image: "14645915394584643.png",
        cusine: "",
        city: "Abu Dhabi",
        lat_long: "24.494183, 54.367608",
        image: "14650440703886102.jpg",
        is_fav: "No"
    },
    {
        distance: "",
        Busid: "56",
        locid: "6312",
        business_name: "Bowling City",
        category_name: "Activities",
        area: "Deira, Dubai",
        cat_image: "14645915394584643.png",
        cusine: "",
        city: "Dubai",
        lat_long: "25.251964, 55.332805",
        image: "14650440703886102.jpg",
        is_fav: "No"
    },
    {
        distance: "",
        Busid: "56",
        locid: "6310",
        business_name: "Bowling City",
        category_name: "Activities & Leisure",
        area: "Al Ain Mall, Al Ain",
        cat_image: "14645915394584643.png",
        cusine: "",
        city: "Al Ain",
        lat_long: "24.222321, 55.782205",
        image: "14650440703886102.jpg",
        is_fav: "No"
    }    
  ]
}

and in the model you would just have a List

0

Gson formatter would solve your problem. You can create model from your json template.

Ahmet NM
  • 73
  • 7
  • I ve tested out your json template with gsonformat, seems json does not properly formatted. [GsonFormat](https://plugins.jetbrains.com/plugin/7654?pr=androidstudio) link. – Ahmet NM Aug 09 '16 at 08:10