Verwenden Sie die Merge-Methode von rxjava, um die von den beiden APIs zurückgegebenen Datenobjekte zu kombinieren und ein Objekt zu erhalten. Anschließend möchte ich die SharedPreferences-Methode verwenden, um das Objekt zwischenzuspeichern.
Ich habe versucht, Objcet mit der Online-Methode zu speichern, aber es hatte keine Wirkung. Bitte helfen Sie mir, einen Blick darauf zu werfen.
Bohne:
public class LifeSuggestionResult implements Serializable{ ... }
public class WeatherFuture implements Serializable { ... }
ObjectUtil (verwenden Sie SharedPreferences, um Object zu speichern und abzurufen):
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-Teil:
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、在其他地方单独使用ObjectUtil,看是否可以存储和取出一个假数据Object
2、在onNext中打印出Object的内容,看是否是预期的Object
3、假如两者都没问题,那就看你的setWeatherInfo方法是否有问题