2016-11-02 97 views
1

api響應(www.railwayapi.com/api/#trains-between-stations)不包含任何PrimaryKey字段(api不在我的控制下),所以我創建了另一個類並且設置PK和對它的迴應。 我經歷了StackOverflow上所有類似的問題,並做了相應的處理。仍然數據(使用Retrofit獲取)重複插入Realm而不是更新。我該如何解決這個問題?Realm android insertOrUpdate not updating

這是我如何保存數據到服務器:(編者)

private void loadJSON(){ 

    SearchResultsApiInterface apiService = SearchResultsApiClient.getClient().create(SearchResultsApiInterface.class); 
    Call<SearchResultsResponse> call = apiService.getTrainNameResponse(SSC,DSC,DATE,API_KEY); 
    call.enqueue(new Callback<SearchResultsResponse>() { 
     @Override 
     public void onResponse(Call<SearchResultsResponse> call, Response<SearchResultsResponse> response) { 

          final SearchResultsResponse jsonResponse = response.body(); 
      jsonResponse.setKey(SSC+DSC+DATE); //EDITED (SSC,DSC,DATE are the EditText valus enter by user) 
      final List<Train> data = jsonResponse.getTrain(); 
      String from = data.get(0).getFrom().getCode(); 
      String to = data.get(0).getTo().getCode(); 
      transaction = realm.executeTransactionAsync(new Realm.Transaction() { 
       @Override 
       public void execute(Realm realm) { 
        realm.insertOrUpdate(jsonResponse); 
       } 
      }, new Realm.Transaction.OnSuccess() { 
       @Override 
       public void onSuccess() { 
        Toast.makeText(getApplicationContext(),"Data saved", Toast.LENGTH_LONG).show();} 
      }, new Realm.Transaction.OnError() { 
       @Override 
       public void onError(Throwable error) { 
        Toast.makeText(getApplicationContext(),"Error in saving",Toast.LENGTH_LONG).show();} 
      }); 
      SearchResultsDataAdapter adapter = new SearchResultsDataAdapter(MainActivity.this,traindata); 
      recyclerView.setAdapter(adapter); 
      String toolbarTitle = from + " " + "-" + " " + to; 
      toolbar.setTitle(toolbarTitle); 
     } 

     @Override 
     public void onFailure(Call<SearchResultsResponse> call, Throwable t) { 
      Log.d("Error", "" + t.getMessage()); 
     } 
    }); 
} 

這是響應:(編者)

package com.android.vyshnav.indianrailwaysimpledesign.modelSearchTrain; 

import java.util.ArrayList; 
import java.util.List; 
import javax.annotation.Generated; 
import io.realm.RealmList; 
import io.realm.RealmObject; 
import io.realm.annotations.PrimaryKey; 

@Generated("org.jsonschema2pojo") 
public class SearchResultsResponse extends RealmObject{ 

@PrimaryKey 
private String key; 
private int total; 
private String error; 
private RealmList<Train> train = new RealmList<Train>(); 
private int response_code; 

public String getKey() { 
    return key; 
} 

public void setKey(String key) { 
    this.key = key; 
} 

/** 
* 
* @return 
* The train 
*/ 
public RealmList<Train> getTrain() { 
    return train; 
} 

/** 
* 
* @param train 
* The train 
*/ 
public void setTrain(RealmList<Train> train) { 
    this.train = train; 
} 

/** 
* 
* @return 
* The responseCode 
*/ 
public int getResponseCode() { 
    return response_code; 
} 

/** 
* 
* @param responseCode 
* The response_code 
*/ 
public void setResponseCode(int responseCode) { 
    this.response_code = responseCode; 
} 

/** 
* 
* @return 
* The total 
*/ 
public int getTotal() { 
    return total; 
} 

/** 
* 
* @param total 
* The total 
*/ 
public void setTotal(int total) { 
    this.total = total; 
} 

/** 
* 
* @return 
* The error 
*/ 
public String getError() { 
    return error; 
} 

/** 
* 
* @param error 
* The error 
*/ 
public void setError(String error) { 
    this.error = error; 
} 


} 

這是JSON響應:

{ 
"response_code": 200, 
"total": 1, 
"train": [ 
    { 
     "no": 1, 
     "name": "RAPTI SAGAR EXP", 
     "number": "12511", 
     "src_departure_time": "06:35", 
     "dest_arrival_time": "03:50", 
     "travel_time": "21:15", 
     "from": { 
      "name": "GORAKHPUR JN", 
      "code": "GKP" 
     }, 
     "to": { 
      "name": "NAGPUR", 
      "code": "NGP" 
     }, 
     "classes": [ 
      { 
       "class-code": "FC", 
       "available": "N" 
      }, 
      { 
       "class-code": "3E", 
       "available": "N" 
      }, 
      { 
       "class-code": "CC", 
       "available": "N" 
      }, 
      { 
       "class-code": "SL", 
       "available": "Y" 
      }, 
      { 
       "class-code": "2S", 
       "available": "N" 
      }, 
      { 
       "class-code": "2A", 
       "available": "Y" 
      }, 
      { 
       "class-code": "3A", 
       "available": "Y" 
      }, 
      { 
       "class-code": "1A", 
       "available": "N" 
      } 
     ], 
     "days": [ 
      { 
       "day-code": "MON", 
       "runs": "N" 
      }, 
      { 
       "day-code": "TUE", 
       "runs": "N" 
      }, 
      { 
       "day-code": "WED", 
       "runs": "N" 
      }, 
      { 
       "day-code": "THU", 
       "runs": "Y" 
      }, 
      { 
       "day-code": "FRI", 
       "runs": "Y" 
      }, 
      { 
       "day-code": "SAT", 
       "runs": "N" 
      }, 
      { 
       "day-code": "SUN", 
       "runs": "Y" 
      } 
     ] 
    } 
] 
} 
+0

不應'wpk.setPk(「從+到」)'是'wpk.setPk(從+到);'? –

+0

你操縱'jsonResponse',但然後插入一個完全不相關的對象? 'realm.insertOrUpdate(wpk);' –

+0

@Christian Melchior在粘貼時出現錯誤...我現在已經更新了...實際的代碼是realm.insertOrUpdate(jsonResponse)。現在請看看它。 –

回答

0

您將得到d因爲SearchResultsResponse沒有主鍵。

基於API你使用

{ 
    "response_code": 200, 
    "error": "", 
    "position": "Train has reached Destination and late by 5 minutes.", 
    "train_number": "12046", 
    "route": [ 
     { 
      "no": 1, 
      "station_": { 
       "name": "CHANDIGARH", 
       "code": "CDG" 
      }, 
      "has_arrived": false, 
      "has_departed": true, 
      "day": 0, 
      "distance": 0, 
      "scharr": "Source", 
      "schdep": "12:00", 
      "actarr": "00:00", 
      "actdep": "12:00", 
      "scharr_date": "19 Nov 2015", 
      "actarr_date": "19 Nov 2015", 
      "latemin": 0 
     }, 
     { 
      "no": 2, 
      "station_": { 
       "name": "AMBALA CANT JN", 
       "code": "UMB" 
      }, 
      "has_arrived": true, 
      "has_departed": true, 
      "day": 0, 
      "distance": 67, 
      "scharr": "12:40", 
      "schdep": "12:42", 
      "actarr": "12:40", 
      "actdep": "12:42", 
      "scharr_date": "19 Nov 2015", 
      "actarr_date": "19 Nov 2015", 
      "latemin": 0 
     }, 
     { 
      "no": 3, 
      "station_": { 
       "name": "NEW DELHI", 
       "code": "NDLS" 
      }, 
      "has_arrived": true, 
      "has_departed": false, 
      "day": 0, 
      "distance": 265, 
      "scharr": "15:25", 
      "schdep": "Destination", 
      "actarr": "15:30", 
      "actdep": "00:00", 
      "scharr_date": "19 Nov 2015", 
      "actarr_date": "19 Nov 2015", 
      "latemin": 5 
     } 
    ] 
} 

對我來說,好像你應該建立以下RealmObject:

public class TrainRoute extends RealmObject { 
    @PrimaryKey 
    private String key; // train_number + station_code + scharr_date + scharr 

    private long trainNumber; 
    private int no; 
    private String stationName; 
    private String stationCode; 
    private boolean hasArrived; 
    private boolean hasDeparted; 
    private int day; 
    private int distance; 
    private String scharr; 
    private String schdep; 
    private String actarr; 
    private String actdep; 
    private String scharrDateText; 
    private String actarrDateText; 
    private Date scharrDate; 
    private Date actarrDate; 
    private int latemin; 

    // getter setter 
} 

之後你的主鍵應該是具體的,一樣的,他們不應該相互覆蓋。

+0

我試過了,但還是一樣的。我已更新上述代碼。請檢查。我錯了我設置PK的方式嗎? –

+0

你甚至不應該有一個'SearchResultsResponse'類 – EpicPandaForce

+0

對不起,我沒有得到你。爲什麼?這是應答課。並且也有內部類。我沒有發佈它們。我是不是該? –