search

Home  >  Q&A  >  body text

android - How to cache the Object object data returned by rxjava merge

Use rxjava's merge method to combine the data objects returned by the two apis to get an Object, and then I want to use the SharedPreferences method to cache the Object.

I tried to save Objcet according to the online method, but it had no effect. Please help me take a look.

Bean:

public class LifeSuggestionResult implements Serializable{ ... }
public class WeatherFuture implements Serializable { ... }

ObjectUtil(Use SharedPreferences to save and get 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 part:

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);
            ...
    }
世界只因有你世界只因有你2758 days ago667

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-16 13:36:50

    Give you an idea, use the elimination method
    1. Use ObjectUtil alone in other places to see if you can store and retrieve a fake data Object
    2. Print the content of the Object in onNext to see if it is the expected Object
    3. If there is no problem with both, then check whether there is a problem with your setWeatherInfo method

    reply
    0
  • Cancelreply