我正在开发Android应用,我想将Retrofit JSON响应保存到RealmDatabase中,但是我无法在response.body上调用getIntroduction方法 我已遵循此link
在我的IntroductionItem.java下面
公共类IntroductionItem扩展了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;
}
}
在我的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();
}