我试图将我的改装对象存储在Realm.io
db中。我已经关注了代码段here,但仍有问题。对此的任何帮助都将非常感激。
我的代码是
自定义GSON
解析器
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.realm.RealmObject;
public class CustomGsonParser {
public static Gson returnCustomParser(){
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.create();
return gson;
}
}
领域呼叫
private void requestData(){
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(getResources().getString(R.string.app_url))
.setLogLevel(RestAdapter.LogLevel.FULL)
.setConverter(new GsonConverter(CustomGsonParser.returnCustomParser()))
.build();
BackendAPI api = adapter.create(BackendAPI.class);
api.getColleges(new Callback<List<College>>() {
@Override
public void success(List<College> colleges, Response response) {
mItems = colleges.toArray(new College[colleges.size()]);
realm.beginTransaction();
List<College> realmObjects = realm.copyToRealmOrUpdate(colleges);
realm.commitTransaction();
}
@Override
public void failure(RetrofitError error) {
System.out.println(error);
}
});
和对象,我省略了setter和getter方法
import java.util.List;
import io.realm.RealmObject;
import io.realm.annotations.Ignore;
public class College extends RealmObject{
private String name;
private String shortName;
private String mainColor;
private String secondaryColor;
private Boolean enabled;
private float latitude;
private float longitude;
private int id;