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"
}
]
}
]
}
不應'wpk.setPk(「從+到」)'是'wpk.setPk(從+到);'? –
你操縱'jsonResponse',但然後插入一個完全不相關的對象? 'realm.insertOrUpdate(wpk);' –
@Christian Melchior在粘貼時出現錯誤...我現在已經更新了...實際的代碼是realm.insertOrUpdate(jsonResponse)。現在請看看它。 –