0
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
    Call<AdvertListResponse> call = apiInterface.getAdvertList();
    call.enqueue(new Callback<AdvertListResponse>() {
        @Override
        public void onResponse(Call<AdvertListResponse> call, Response<AdvertListResponse> response) {
            Log.i("ssss",""+response.body());
            AdvertListResponse listResponse = response.body();
            DataAdvertList dataAdvertList = listResponse.getDataAdvertLists().get(0);
            List<InfoAdvertList> advertList = dataAdvertList.getInfoAdvertLists();
            Log.i("ssss","advert"+advertList);
            boolean succes = dataAdvertList.isSuccess();
            if(succes){
                Log.i("ssss",""+succes);
                Realm realm = Realm.getDefaultInstance();
                realm.beginTransaction();
                realm.copyToRealmOrUpdate(advertList);
                realm.commitTransaction();
            }else {
                String error = dataAdvertList.getErrors().get(0);
                Toast.makeText(AdvertActivity.this,"error"+error,Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<AdvertListResponse> call, Throwable t) {
            Log.i("ssss",t.toString());
            Toast.makeText(AdvertActivity.this,t.toString(),Toast.LENGTH_SHORT).show();
        }
    });

public class AdvertListResponse {
@SerializedName("data")
@Expose
private List<DataAdvertList> dataAdvertLists = new ArrayList<DataAdvertList>();

public List<DataAdvertList> getDataAdvertLists() {
    return dataAdvertLists;
}

public void setDataAdvertLists(List<DataAdvertList> dataAdvertLists) {
    this.dataAdvertLists = dataAdvertLists;
}

}

public class DataAdvertList {

@SerializedName("success")
@Expose
private boolean success;
@SerializedName("object")
@Expose
private List<InfoAdvertList> infoAdvertLists;
@SerializedName("errors")
@Expose
private List<String> errors;

public boolean isSuccess() {
    return success;
}

public void setSuccess(boolean success) {
    this.success = success;
}

public List<InfoAdvertList> getInfoAdvertLists() {
    return infoAdvertLists;
}

public void setInfoAdvertLists(List<InfoAdvertList> infoAdvertLists) {
    this.infoAdvertLists = infoAdvertLists;
}

public List<String> getErrors() {
    return errors;
}

public void setErrors(List<String> errors) {
    this.errors = errors;
}

}

public class InfoAdvertList extends RealmObject{
@PrimaryKey
@SerializedName("id")
@Expose
private String id;
@SerializedName("cat_id")
@Expose
private String catId;
@SerializedName("is_dogovornaja")
@Expose
private String isDogovornaja;
@SerializedName("is_obmen")
@Expose
private String isObmen;
@SerializedName("description")
@Expose
private String description;
@SerializedName("email")
@Expose
private String email;
@SerializedName("city_id")
@Expose
private String cityId;
@SerializedName("address")
@Expose
private String address;
@SerializedName("phone1")
@Expose
private String phone1;
@SerializedName("phone2")
@Expose
private String phone2;
@SerializedName("phone3")
@Expose
private String phone3;
@SerializedName("phone4")
@Expose
private String phone4;
@SerializedName("phone5")
@Expose
private String phone5;
@SerializedName("coords")
@Expose
private String coords;
@SerializedName("is_new")
@Expose
private String isNew;
@SerializedName("mark_id")
@Expose
private String markId;
@SerializedName("model_id")
@Expose
private String modelId;
@SerializedName("user_id")
@Expose
private String userId;
@SerializedName("price")
@Expose
private String price;
@SerializedName("created")
@Expose
private String created;
@SerializedName("status")
@Expose
private String status;
@SerializedName("is_favourite")
@Expose
private Integer isFavourite;
@SerializedName("modelname")
@Expose
private String modelname;
@SerializedName("markname")
@Expose
private String markname;
@SerializedName("photos")
@Expose
private RealmList<PhotoAdvert> photos;

public String getId() {
    return id;
}

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

public String getCatId() {
    return catId;
}

public void setCatId(String catId) {
    this.catId = catId;
}

public String getIsDogovornaja() {
    return isDogovornaja;
}

public void setIsDogovornaja(String isDogovornaja) {
    this.isDogovornaja = isDogovornaja;
}

public String getIsObmen() {
    return isObmen;
}

public void setIsObmen(String isObmen) {
    this.isObmen = isObmen;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getCityId() {
    return cityId;
}

public void setCityId(String cityId) {
    this.cityId = cityId;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getPhone1() {
    return phone1;
}

public void setPhone1(String phone1) {
    this.phone1 = phone1;
}

public String getPhone2() {
    return phone2;
}

public void setPhone2(String phone2) {
    this.phone2 = phone2;
}

public String getPhone3() {
    return phone3;
}

public void setPhone3(String phone3) {
    this.phone3 = phone3;
}

public String getPhone4() {
    return phone4;
}

public void setPhone4(String phone4) {
    this.phone4 = phone4;
}

public String getPhone5() {
    return phone5;
}

public void setPhone5(String phone5) {
    this.phone5 = phone5;
}

public String getCoords() {
    return coords;
}

public void setCoords(String coords) {
    this.coords = coords;
}

public String getIsNew() {
    return isNew;
}

public void setIsNew(String isNew) {
    this.isNew = isNew;
}

public String getMarkId() {
    return markId;
}

public void setMarkId(String markId) {
    this.markId = markId;
}

public String getModelId() {
    return modelId;
}

public void setModelId(String modelId) {
    this.modelId = modelId;
}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getCreated() {
    return created;
}

public void setCreated(String created) {
    this.created = created;
}

public String getStatus() {
    return status;
}

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

public Integer getIsFavourite() {
    return isFavourite;
}

public void setIsFavourite(Integer isFavourite) {
    this.isFavourite = isFavourite;
}

public RealmList<PhotoAdvert> getPhotos() {
    return photos;
}

public void setPhotos(RealmList<PhotoAdvert> photos) {
    this.photos = photos;
}

public String getModelname() {
    return modelname;
}

public void setModelname(String modelname) {
    this.modelname = modelname;
}

public String getMarkname() {
    return markname;
}

public void setMarkname(String markname) {
    this.markname = markname;
}

}

Вот такая ошибка выходить com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 38 path $.data[0].object[0]

Вот json ответ

{"data":[{"success":true,"object":[{"id":"2","cat_id":"1","is_dogovornaja":"1","is_obmen":"1","description":"\u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u043e\u0432\u0430\u0440\u0430","email":"test@gmail.com","city_id":"1","address":"\u0433. \u0410\u043b\u043c\u0430\u0442\u044b, \u0443\u043b. \u0428\u0430\u0448\u043a\u0438\u043d\u0430 54","phone1":"(777)123-4567","phone2":"(777)423-3567","phone3":"(777)523-2567","phone4":"(777)323-1567","phone5":"(777)723-0567","coords":"44.2536545, 46.525656332","is_new":"1","mark_id":"1","model_id":"1","user_id":"54","price":"100000","created":"1481446357","status":"1","is_favourite":0,"modelname":"Samsung Galaxy S7","markname":"Samsung","photos":[{"id":"1","url":".\/uploads\/ob\/3ece2520bd8a1f36a16983ffc52d8ad8_0.jpg"},{"id":"2","url":".\/uploads\/ob\/3ece2520bd8a1f36a16983ffc52d8ad8_1.jpg"}]}],"errors":[]}]}

1 ответ 1

0

У вас data это один объект DataAdvertList, вы пытаетесь добыть массив.

Вместо

public List<DataAdvertList> getDataAdvertLists() {
    return dataAdvertLists;
} 

и

@SerializedName("data")
@Expose
private List<DataAdvertList> dataAdvertLists = new ArrayList<DataAdvertList>();

и

DataAdvertList dataAdvertList = listResponse.getDataAdvertLists().get(0);

Напишите так:

public DataAdvertList getDataAdvertLists() {
    return dataAdvertLists;
}

и

@SerializedName("data")
@Expose
private DataAdvertList dataAdvertLists;

и

DataAdvertList dataAdvertList = listResponse.getDataAdvertLists();
1
  • Но у меня ошибка не в date, а object в классе InfoAdvertList.Если поменять как вы сказали то выходит ошибка в date Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 10 path $.data
    – Mr.Baga
    14 дек 2016 в 4:53

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.