1

How to make model class in java android for json in retrofit I have this json

Json respons data model

I have make six model class in java in android. which are as follows

model 1 :- EventsResponseData.java

package com.galaktiknepal.eticketnepal.Models.NetworkingModels;

import com.google.gson.annotations.SerializedName;

public class EventsResponseData {

    @SerializedName("data")
    EventsData data;

    @SerializedName("status")
    boolean status;

    @SerializedName("categories")
    EventsCategories eventsCategories;

    public EventsData getData() {
        return data;
    }

    public void setData(EventsData data) {
        this.data = data;
    }

    public Boolean getStatus() {
        return status;
    }

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

    public EventsCategories getEventsCategories() {
        return eventsCategories;
    }

    public void setEventsCategories(EventsCategories eventsCategories) {
        this.eventsCategories = eventsCategories;
    }
}

model 2 :-EventsData.java

package com.galaktiknepal.eticketnepal.Models.NetworkingModels;

import com.google.gson.annotations.SerializedName;

public class EventsData {

    @SerializedName("id")
    int id;

    @SerializedName("title")
    String title;

    @SerializedName("slug")
    String slug;

    @SerializedName("image")
    EventsImage image;

    @SerializedName("about")
    String about;

    @SerializedName("least_price")
    String least_price;

    @SerializedName("date_count")
    int date_count;

    @SerializedName("least_date")
    EventsLeastDate least_date;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSlug() {
        return slug;
    }

    public void setSlug(String slug) {
        this.slug = slug;
    }

    public EventsImage getImage() {
        return image;
    }

    public void setImage(EventsImage image) {
        this.image = image;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public String getLeast_price() {
        return least_price;
    }

    public void setLeast_price(String least_price) {
        this.least_price = least_price;
    }

    public int getDate_count() {
        return date_count;
    }

    public void setDate_count(int date_count) {
        this.date_count = date_count;
    }

    public EventsLeastDate getLeast_date() {
        return least_date;
    }

    public void setLeast_date(EventsLeastDate least_date) {
        this.least_date = least_date;
    }
}

model 3 :- EventsImage.java

package com.galaktiknepal.eticketnepal.Models.NetworkingModels;

import com.google.gson.annotations.SerializedName;

public class EventsImage {

    @SerializedName("cropped")
        String cropped;

    public String getCropped() {
        return cropped;
    }

    public void setCropped(String cropped) {
        this.cropped = cropped;
    }

}

model 4:- EventsLeastDate.java

package com.galaktiknepal.eticketnepal.Models.NetworkingModels;

import com.google.gson.annotations.SerializedName;

public class EventsLeastDate {

    @SerializedName("human")
    String human;

    @SerializedName("iso")
    String iso;

    @SerializedName("formatted")
    String formatted;

    @SerializedName("formatted_trim")
    String formatted_trim;

    public String getHuman() {
        return human;
    }

    public void setHuman(String human) {
        this.human = human;
    }

    public String getIso() {
        return iso;
    }

    public void setIso(String iso) {
        this.iso = iso;
    }

    public String getFormatted() {
        return formatted;
    }

    public void setFormatted(String formatted) {
        this.formatted = formatted;
    }

    public String getFormatted_trim() {
        return formatted_trim;
    }

    public void setFormatted_trim(String formatted_trim) {
        this.formatted_trim = formatted_trim;
    }

}

model 5 :- EventsCategories.java

package com.galaktiknepal.eticketnepal.Models.NetworkingModels;

import com.google.gson.annotations.SerializedName;

public class EventsCategories {

    @SerializedName("id")
    int id;

    @SerializedName("sub_id")
    int sub_id;

    @SerializedName("name")
    String name;

    @SerializedName("icon")
    String icon;

    @SerializedName("slug")
    String slug;

    @SerializedName("seo")
    EventsSeo seo;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getSub_id() {
        return sub_id;
    }

    public void setSub_id(int sub_id) {
        this.sub_id = sub_id;
    }

    public String getName() {
        return name;
    }

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

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getSlug() {
        return slug;
    }

    public void setSlug(String slug) {
        this.slug = slug;
    }

    public EventsSeo getSeo() {
        return seo;
    }

    public void setSeo(EventsSeo seo) {
        this.seo = seo;
    }
}

Model 6:- EventsSeo.java

package com.galaktiknepal.eticketnepal.Models.NetworkingModels;

import com.google.gson.annotations.SerializedName;

public class EventsSeo {

    @SerializedName("keywords")
    String keywords;

    @SerializedName("desc")
    String desc;

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

And this is my retrofit api

@POST("event/all")
Call<EventsResponseData> getEventsAllData();

And this is my Retrofit Call

private void GetAllEvents(){
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.eticketnepal.com/api/")
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
    JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);

    Log.e("testing : ","begain");
    Call<EventsResponseData> call = jsonPlaceHolderApi.getEventsAllData();
    call.enqueue(new Callback<EventsResponseData>() {
        @Override
        public void onResponse(Call<EventsResponseData> call, Response<EventsResponseData> response) 
{
            if(response.isSuccessful()){

              //  currentEventsResponseData=response.body();
                Log.e("event success"," "+response.body().toString());

            }else{

               Log.e("Message","sorry!.......we are unable to fetch user info .......");
            }
        }

        @Override
        public void onFailure(Call<EventsResponseData> call, Throwable t) {
            Log.e("Failure msg"," "+t.getMessage()+" / "+t.getCause());
        }
    });
}

My code throws me this error E/Failure msg: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 10 path $.data

please help me to fix this problem

  • Possible duplicate of [GSON throwing "Expected BEGIN\_OBJECT but was BEGIN\_ARRAY"?](https://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array) – Bruno Nov 28 '19 at 13:44
  • Visit the website http://www.jsonschema2pojo.org, paste your `JSON response` and enjoy – Aspicas Nov 28 '19 at 13:45

1 Answers1

1

Change your EventsResponseData class, you are waiting object but in response api returning array of objects

public class EventsResponseData {

@SerializedName("data")
List<EventsData> data;

@SerializedName("status")
boolean status;

@SerializedName("categories")
List<EventsCategories> eventsCategories;

public EventsData getData() {
    return data;
}

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

public Boolean getStatus() {
    return status;
}

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

public EventsCategories getEventsCategories() {
    return eventsCategories;
}

public void setEventsCategories(List<EventsCategories> eventsCategories) {
    this.eventsCategories = eventsCategories;
}

}

Jamshid
  • 101
  • 1
  • 2