通过Retrofit 2将数据存储到Realm中

时间:2017-08-23 09:19:38

标签: json database gson realm retrofit2

我是Realm集成的新手。我试图将我的改造json响应保存到Realm数据库中。但是我仍然混淆了如何完成改造2的这项任务。

我正在获取json响应并根据RealM文档扩展RealmObject类。但仍然没有任何好的方法直接将我的回复存储到领域。

信息中心活动:

declare @a varchar(255)='EREF+322345 KARR CUSTOMER1'
select substring(@a,charindex('+',@a,1)+1,charindex(' ',@a,1)-charindex('+',@a,1))

-------------------------------
declare @a varchar(255)='EREF+3211234 KARR CUSTROMER2.'
select substring(@a,charindex('+',@a,1)+1,charindex(' ',@a,1)-charindex('+',@a,1))

API客户端:

  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 ");

            }

的build.gradle:

{ 
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;
}

POJO课程:

ExampleTest:

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'

ListQuestion:

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;
        }

  }

RealM:

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;
    }

请帮助我如何通过改造2将我的json响应存储到领域。

1 个答案:

答案 0 :(得分:4)

我从Realm官方网站和其他博客得到了答案。

我的上述代码工作正常。只需要正确添加Realm功能。

在活动:仪表板

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

                resource = response.body();


              Realm.init(DashboardActivity.this);
              config = new RealmConfiguration.Builder()
                        .name("books.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();


// programmatically check : data is inserted in to realm or not

                int notesCount = realm.where(ExampleTest.class).findAll().size();
                int notesCount2 = realm.where(ListConditionalQuestion.class).findAll().size();
                int notesCount3 = realm.where(LstHotSpot.class).findAll().size();

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


            }


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

   Log.d("fail","response fail");

            }
        });