Хранение данных через Realm через Retrofit - PullRequest
0 голосов
/ 02 ноября 2018

Я занимаюсь разработкой приложения для Android. Я хочу сохранить ответ Retrofit JSON в RealmDatabase, но не могу вызвать метод getIntroduction для response.body Я перешел по этой ссылке

ниже моего ВведениеItem.java

открытый класс. ВведениеItem расширяет AppCompatActivity {

public List<Introduction> introductionList;
public IntroductionAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.introduction);
    KitabInterface kitabInterface = ApiClient.getApiService();
    Call<KitabSawti> call = kitabInterface.getIntroduction();

    call.enqueue(new Callback<RealmList<KitabSawti>>() {
        @Override
        public void onResponse(Call<RealmList<KitabSawti>> call, Response<RealmList<KitabSawti>> response) {
            introductionList =  response.body().getIntroduction;
            Realm.init(IntroductionItem.this);

            RealmConfiguration config = new RealmConfiguration.Builder()
                    .name("overview.realm")
                    .schemaVersion(1)
                    .deleteRealmIfMigrationNeeded()
                    .build();
            Realm.setDefaultConfiguration(config);


            // add response to realm database
            Realm realm = Realm.getInstance(config);
            realm.beginTransaction();
            realm.copyToRealmOrUpdate(resource);
            realm.commitTransaction();
            realm.close();

// программная проверка: данные вставлены в область или нет

            int notesCount = realm.where(KitabSawti.class).findAll().size();
            int notesCount2 = realm.where(Introduction.class).findAll().size();


            Log.d("my first",String.valueOf(notesCount));
            Log.d("my second",String.valueOf(notesCount2));





            RecyclerView recyclerView  =  findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            adapter = new IntroductionAdapter(IntroductionItem.this,introductionList); // changes
            recyclerView.setAdapter(adapter);
        }

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

        }
    });
}

}

Класс Pojo: KitabSawti

открытый класс KitabSawti расширяет RealmObject {

@SerializedName("Introduction")
@Expose
private RealmList<Introduction> introduction = null;
@SerializedName("Education")
@Expose
private List<Education> education = null;
@SerializedName("Work")
@Expose
private List<Work> work = null;
@SerializedName("Skills")
@Expose
private List<Skill> skills = null;
@SerializedName("Contact")
@Expose
private List<Contact> contact = null;

public RealmList<Introduction> getIntroduction() {
    return introduction;
}

public void setIntroduction(RealmList<Introduction> introduction) {
    this.introduction = introduction;
}

public List<Education> getEducation() {
    return education;
}

public void setEducation(List<Education> education) {
    this.education = education;
}

public List<Work> getWork() {
    return work;
}

public void setWork(List<Work> work) {
    this.work = work;
}

public List<Skill> getSkills() {
    return skills;
}

public void setSkills(List<Skill> skills) {
    this.skills = skills;
}

public List<Contact> getContact() {
    return contact;
}

public void setContact(List<Contact> contact) {
    this.contact = contact;
}

}

Introduction.java

открытый класс Введение расширяет RealmObject {

@SerializedName("image")
@Expose
private String image;
@SerializedName("introduction")
@Expose
private String introduction;

public String getImage() {
    return image;
}

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

public String getIntroduction() {
    return introduction;
}

public void setIntroduction(String introduction) {
    this.introduction = introduction;
}

}

ниже скриншот ошибка, где я получаю error

ниже моего ApiClient

публичный класс ApiClient

{


        public static final String BASE_URL = "http://www.mocky.io/";

        /**
         * Get Retrofit Instance
         */


        public static Retrofit getRetrofitInstance() {

            return new Retrofit.Builder()

                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

        }

        /**
         * Get API Service
         *
         * @return API Service
         */
        public static KitabInterface getApiService() {
            return getRetrofitInstance().create(KitabInterface.class);
        }
    }

ниже интерфейса

public interface KitabInterface {

    @GET("v2/5bd2e8ec3400006f00cfdf42")
    Call<KitabSawti> getIntroduction();
    @GET("v2/5bd2e8ec3400006f00cfdf42")
    Call<KitabSawti> getEducation();
    @GET("v2/5bd2e8ec3400006f00cfdf42")
    Call<KitabSawti> getWork();
    @GET("v2/5bd2e8ec3400006f00cfdf42")
    Call<KitabSawti> getSkills();
    @GET("v2/5bd2e8ec3400006f00cfdf42")
    Call<KitabSawti> getContact();





}
...