2016-11-02 61 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

对不起,我没有得到你。为什么?这是应答课。并且也有内部类。我没有发布它们。我是不是该? –