1

i am using this json http://jsoneditoronline.org/?id=8fac3ea95398ed23cbe471e1f277d925 to fetch results with retrofit and persist data with green dao, but i keep getting this exception

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 150 path $.results[0].merits[0] 

here is my pojo class

@Entity
public class AllJobsModel {

    @Id
    private Long id;
    @SerializedName("brand_name")
    private String brand_name;
    @SerializedName("subcategory")
    private int subcategory;
    @SerializedName("hourly_salary_min")
    private int hourly_salary_min;
    @SerializedName("hourly_salary_max")
    private int hourly_salary_max;
    @SerializedName("station")
    private String station;
    @SerializedName("view_count")
    private int view_count;
    @SerializedName("is_saved")
    private boolean is_saved;
    @SerializedName("is_applied")
    private boolean is_applied;
    @SerializedName("posted_at")
    private String posted_at;

    @ToMany(referencedJoinProperty = "merit_id")
    @SerializedName("merits")
    private List<JobMerits> merits;
    /**
     * Used to resolve relations
     */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;
    /**
     * Used for active entity operations.
     */
    @Generated(hash = 1096418661)
    private transient AllJobsModelDao myDao;

    @Generated(hash = 233816570)
    public AllJobsModel(Long id, String brand_name, int subcategory, int hourly_salary_min,
                        int hourly_salary_max, String station, int view_count, boolean is_saved,
                        boolean is_applied, String posted_at) {
        this.id = id;
        this.brand_name = brand_name;
        this.subcategory = subcategory;
        this.hourly_salary_min = hourly_salary_min;
        this.hourly_salary_max = hourly_salary_max;
        this.station = station;
        this.view_count = view_count;
        this.is_saved = is_saved;
        this.is_applied = is_applied;
        this.posted_at = posted_at;
    }

    @Generated(hash = 5603562)
    public AllJobsModel() {
    }

    public Long getId() {
        return id;
    }

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

    public String getBrand_name() {
        return brand_name;
    }

    public void setBrand_name(String brand_name) {
        this.brand_name = brand_name;
    }

    public int getSubcategory() {
        return subcategory;
    }

    public void setSubcategory(int subcategory) {
        this.subcategory = subcategory;
    }

    public int getHourly_salary_min() {
        return hourly_salary_min;
    }

    public void setHourly_salary_min(int hourly_salary_min) {
        this.hourly_salary_min = hourly_salary_min;
    }

    public int getHourly_salary_max() {
        return hourly_salary_max;
    }

    public void setHourly_salary_max(int hourly_salary_max) {
        this.hourly_salary_max = hourly_salary_max;
    }

    public String getStation() {
        return station;
    }

    public void setStation(String station) {
        this.station = station;
    }

    public int getView_count() {
        return view_count;
    }

    public void setView_count(int view_count) {
        this.view_count = view_count;
    }

    public boolean isIs_saved() {
        return is_saved;
    }

    public void setIs_saved(boolean is_saved) {
        this.is_saved = is_saved;
    }

    public boolean isIs_applied() {
        return is_applied;
    }

    public void setIs_applied(boolean is_applied) {
        this.is_applied = is_applied;
    }


    public String getPosted_at() {
        return posted_at;
    }

    public void setPosted_at(String posted_at) {
        this.posted_at = posted_at;
    }

    public boolean getIs_saved() {
        return this.is_saved;
    }

    public boolean getIs_applied() {
        return this.is_applied;
    }


@Entity
public class JobMerits {
    private Long merit_id;

    @Generated(hash = 129169963)
    public JobMerits(Long merit_id) {
        this.merit_id = merit_id;
    }

    @Generated(hash = 2038021091)
    public JobMerits() {
    }

    public Long getMerit_id() {
        return merit_id;
    }

    public void setMerit_id(Long merit_id) {
        this.merit_id = merit_id;
    }
}

this is how i am making a retrofit call

public interface ApiInterface {

    @GET("plzw3")
    Call<ResponseMode> getAllJobs();


    @GET("top-headlines?sources=techcrunch&apiKey=801d8c3455fe475bbab9f7e4c6944aa3")
    Call<ResponseMode> getLatestNews();


}




public class ResponseMode {

    @SerializedName("results")
    private List<AllJobsModel> allJobsModelList;

    public List<AllJobsModel> getAllJobsModelList() {
        return allJobsModelList;
    }

    public void setAllJobsModelList(List<AllJobsModel> allJobsModelList) {
        this.allJobsModelList = allJobsModelList;
    }
}


public class ApiClient {

//    public static final String BASE_URL = "https://newsapi.org/v2/";
    public static final String BASE_URL = "https://api.myjson.com/bins/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}




  private void fetchJobs() {
        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

        Call<ResponseMode> call = apiService.getAllJobs();

        call.enqueue(new Callback<ResponseMode>() {
            @Override
            public void onResponse(Call<ResponseMode> call, Response<ResponseMode> response) {

                // data parsing


                List<AllJobsModel> news = response.body().getAllJobsModelList();


                // inserting data into database
                allJobsModelDao.insertOrReplaceInTx(news);

                // setting up adapter
                setAdapter();
            }

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

            }


        });
    }

what can i do tio fix this ?? any help is highly appriciated

1 Answers1

1

Try changing to private List<Long> merits;

Your code would work if response is like this

 "merits":[
     {
         "merit_id":47
     },
     {
         "merit_id":51
     },
     {
         "merit_id":56
     }
 ]
aloj
  • 3,194
  • 7
  • 27
  • 38