我是rxJava和Retrofit2的新手,但我似乎不知道如何实现理想的情况:
JSON数组包含数百个我希望缓存的“位置”(Singleton),因此我不必对应用程序中的每个更改进行调用。
接口(myAPI)
/**
* RxJava testing endpoint
* @return Observable list of Places
* count = 0 means everything
*/
@GET("place/search?count=0")
rx.Observable<List<Place>> loadPlacesRx();
辛格尔顿
...
private static PlaceSingleton ourInstance = new PlaceSingleton();
private static RxJavaCallAdapterFactory rxAdapter =
RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io());
private static Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(JacksonConverterFactory.create())
.addCallAdapterFactory(rxAdapter)
.baseUrl(myAPI.BASE_URL)
.build();
// Prepare call in Retrofit2
private static MyAPI myAPI = retrofit.create(MyAPI.class);
/**
* This method should handle the steps mentioned above
*/
public void getAllPlaces() {
poiList = myAPI.loadPlacesRx()
.map(Place -> ...
...
发布于 2016-10-25 13:23:14
可以使用Gson将对象保存到自己的缓存序列化对象到字符串中,并将其保存到共享首选项中。
使用Gson:序列化:
new Gson().toJson(yourObject);
并进行反序列化:
new Gson().fromJson(stringObject);
使用Jakson:
序列化:
ObjectMapper mapper = new ObjectMapper();
String objectString = mapper.writeValueAsString(object);
去序列化:
ObjectMapper mapper = new ObjectMapper();
Object object = mapper.readValue(jsonInString, Object.class);
https://stackoverflow.com/questions/40241032
复制相似问题