android - rxjava merge 返回Object對(duì)象數(shù)據(jù)如何緩存
問題描述
使用 rxjava 的 merge 方法將兩個(gè) api 返回的數(shù)據(jù)對(duì)象結(jié)合得到 Object,然后我想使用 SharedPreferences 方法緩存Object。
我嘗試按照網(wǎng)上的方法保存Objcet,卻沒有效果。請(qǐng)大伙幫忙看看。
Bean:
public class LifeSuggestionResult implements Serializable{ ... }public class WeatherFuture implements Serializable { ... }
ObjectUtil( 使用 SharedPreferences 保存和獲取 Object):
public class ObjectUtil { public static void setObject(String key, Object object, Context context) {SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();ByteArrayOutputStream baos = new ByteArrayOutputStream();ObjectOutputStream out = null;try { out = new ObjectOutputStream(baos); out.writeObject(object); String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT)); editor.putString(key, objectVal); editor.commit();} catch (IOException e) { e.printStackTrace();} finally { try {if (baos != null) { baos.close();}if (out != null) { out.close();} } catch (IOException e) {e.printStackTrace(); }} } public static <T> T getObject(String key, Context context) {SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);if (sp.contains(key)) { String objectVal = sp.getString(key, null); byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT); ByteArrayInputStream bais = new ByteArrayInputStream(buffer); ObjectInputStream ois = null; try {ois = new ObjectInputStream(bais);T t = (T) ois.readObject();return t; } catch (StreamCorruptedException e) {e.printStackTrace(); } catch (IOException e) {e.printStackTrace(); } catch (ClassNotFoundException e) {e.printStackTrace(); } finally {try { if (bais != null) {bais.close(); } if (ois != null) {ois.close(); }} catch (IOException e) { e.printStackTrace();} }}return null; }}
rxjava部分:
public void getCurrentWeather(final String city) {Observable<WeatherFuture> weatherFutureObservable = new WeatherService().getFutureWeather(city, 'zh-Hans', 'c');Observable<LifeSuggestionResult> lifeSuggestionResultObservable = new WeatherService().getAirQuality(city, 'zh-Hans', 'city');Observable.merge(weatherFutureObservable, lifeSuggestionResultObservable).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Object>() { @Override public void onCompleted() { } @Override public void onNext(Object o) {setWeatherInfo(o); } @Override public void onError(Throwable e) { }}); } public void setWeatherInfo(Object o) { if (o instanceof WeatherFuture) { ObjectUtil.setObject('WeatherFuture', o, MainActivity.this); ... }} else if (o instanceof LifeSuggestionResult) { ObjectUtil.setObject('LifeSuggestion', o, MainActivity.this); ... }
問題解答
回答1:給你一個(gè)思路,使用排除法1、在其他地方單獨(dú)使用ObjectUtil,看是否可以存儲(chǔ)和取出一個(gè)假數(shù)據(jù)Object2、在onNext中打印出Object的內(nèi)容,看是否是預(yù)期的Object3、假如兩者都沒問題,那就看你的setWeatherInfo方法是否有問題
相關(guān)文章:
1. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題2. win10系統(tǒng) php安裝swoole擴(kuò)展3. extra沒有加載出來4. mysql - php 如何網(wǎng)址中出現(xiàn)該頁標(biāo)題?5. Span標(biāo)簽6. 關(guān)于Mysql數(shù)據(jù)表行轉(zhuǎn)列7. django進(jìn)行數(shù)據(jù)庫的查詢8. PHP求助,求幫忙謝謝各位9. javascript - 釘釘?shù)膃xcel, word文件預(yù)覽是直接用的微軟的office web app,不犯法嗎?10. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時(shí)間會(huì)消失是什么情況?
