通过Retrofit 2将数据存储到Realm中 [英] Store data into Realm through Retrofit 2

查看:122
本文介绍了通过Retrofit 2将数据存储到Realm中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Realm集成的新手.我正在尝试将翻新的json响应保存到Realm数据库中.但我仍然困惑如何完成此任务以进行翻新2.

I am new for Realm integration. I am trying to save my retrofit json response into Realm database. But still I am confuse how to complete this task for retrofit 2.

我收到json响应,并根据RealM文档扩展了RealmObject类.但仍然没有获得将我的响应直接存储到领域中的好方法.

I am getting json response and extends RealmObject class as per RealM documents. but still not getting any good way to store my response into realm directly.

仪表板活动:

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

RealM:

private void initRealm() {
        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name("books.realm")
                .schemaVersion(1)
                .deleteRealmIfMigrationNeeded()
                .build();
        Realm.setDefaultConfiguration(config);


    }

请帮助我如何通过翻新2将json响应存储到领域中.

Please help me how can I store my json response into realm through retrofit 2.

推荐答案

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

I got my answer from Realm official website and other blogs.

我上面的代码运行完美.只需适当添加Realm功能即可.

My above code is working perfect. Just need to add Realm functionality properly.

活动中:仪表板

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

            }
        });

这篇关于通过Retrofit 2将数据存储到Realm中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆