0
我是Realm集成的新成员。我正在尝试将我的翻新JSON响应保存到Realm数据库中。但仍然我很困惑如何完成这项任务的改造2.通过改造将数据存储到领域2
我得到json响应和扩展RealmObject类的RealM文件。但仍然没有任何好的方式直接将我的回复存储到领域。
控制板的活动:
apiInterface = APIClient.getClient().create(APIInterface.class);
final Call<RealmList<ExampleTest>> call = apiInterface.getSurvay();
call.enqueue(new Callback<RealmList<ExampleTest>>() {
@Override
public void onResponse(Call<RealmList<ExampleTest>> call, Response<RealmList<ExampleTest>> response) {
resource = response.body();
Log.d(" response "," success ");
}
API客户端:
{
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl("** my URL **")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
的build.gradle:
compile ('com.squareup.retrofit2:retrofit:2.1.0') {
exclude module: 'okhttp'
}
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
POJO类:
ExampleTest:
public class ExampleTest extends RealmObject {
@SerializedName("Id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String surveyName;
@SerializedName("Language_id")
@Expose
private Integer languageId;
@SerializedName("image")
@Expose
private String surveyImage;
@SerializedName("description")
@Expose
private String surveyDescription;
@SerializedName("ListQuestion")
@Expose
private RealmList<ListQuestion> listQuestion = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSurveyName() {
return surveyName;
}
public void setSurveyName(String surveyName) {
this.surveyName = surveyName;
}
public Integer getLanguageId() {
return languageId;
}
public void setLanguageId(Integer languageId) {
this.languageId = languageId;
}
public String getSurveyImage() {
return surveyImage;
}
public void setSurveyImage(String surveyImage) {
this.surveyImage = surveyImage;
}
public String getSurveyDescription() {
return surveyDescription;
}
public void setSurveyDescription(String surveyDescription) {
this.surveyDescription = surveyDescription;
}
public RealmList<ListQuestion> getListQuestion() {
return listQuestion;
}
public void setListQuestion(RealmList<ListQuestion> listQuestion) {
this.listQuestion = listQuestion;
}
}
ListQuestion:
public class ListQuestion extends RealmObject {
@SerializedName("Id")
@Expose
private Integer id;
@SerializedName("Question_text")
@Expose
private String questionText;
@SerializedName("Answer_type_id")
@Expose
private Integer answerTypeId;
@SerializedName("Answer_type")
@Expose
private String answerType;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getQuestionText() {
return questionText;
}
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
public Integer getAnswerTypeId() {
return answerTypeId;
}
public void setAnswerTypeId(Integer answerTypeId) {
this.answerTypeId = answerTypeId;
}
public String getAnswerType() {
return answerType;
}
public void setAnswerType(String answerType) {
this.answerType = answerType;
}
境界:
private void initRealm() {
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.name("books.realm")
.schemaVersion(1)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
}
请帮助我如何通过改进2将我的json响应存储到域中。
您可以在我们的文档中看到一个例子在这里:https://realm.io/docs/java/latest/#retrofit –
@ChristianMelchior我检查你的链接,但仍没有取得任何成功。 – AppleDroid
@ChristianMelchior请检查以上代码,我犯了什么错误?我怎样才能将我的回复存储到领域。请给我任何你有的链接。非常感谢您的回复。 – AppleDroid