2017-08-23 93 views
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響應存儲到域中。

+0

您可以在我們的文檔中看到一個例子在這裏:https://realm.io/docs/java/latest/#retrofit –

+0

@ChristianMelchior我檢查你的鏈接,但仍沒有取得任何成功。 – AppleDroid

+0

@ChristianMelchior請檢查以上代碼,我犯了什麼錯誤?我怎樣才能將我的回覆存儲到領域。請給我任何你有的鏈接。非常感謝您的回覆。 – AppleDroid

回答

2

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

      } 
     });