可观察的RX- Retrofit仅首次获取数据

时间:2016-01-07 22:48:40

标签: android retrofit rx-java observable retrofit2

这是一个非常奇怪的问题。

我正在使用RX JAVA Observable进行改造以从服务器获取数据,这些数据每次都应该更改,但它似乎只是第一次加载数据,其余时间重新获取相同的数据。

同样没有INTERNET CONNECTION仍然可以获取数据(显然来自本地)并仍然进入onNext,而不是onError。

这是我的观察结果:

Token token = new Token(AppConfig.TOKEN_NEWS);
    Observable<ArrayList<NewsV0>> newsList = mZappAppApis.getNews(token.getToken());
    newsList
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<ArrayList<NewsV0>>() {
                @Override
                public void onCompleted() {
                    Logger.i("Retriving user data onCompleted");
                    onLoadCompleted(false);
                }

                @Override
                public void onError(Throwable e) {

                    Logger.e("Error retriving user data [" + e.getMessage() + "]");
                    onLoadCompleted(true);
                }

                @Override
                public void onNext(ArrayList<NewsV0> newsV0s) {
                    Logger.i("Get news from api size ["+newsV0s.size()+"]");


                    //Every time go here, with internet and without internet, loading the same data
                }
            });

我的改造api:

@GET("/News")
Observable<ArrayList<NewsV0>> getNews(
        @Header("Authorization") String authorization
);

我希望有人可以帮助我!

提前致谢!!

编辑:

这是我提供RetrofitAdapter的方式

@Provides
ZappAppApis provideContentApi(OkHttpClient okHttpClient) {
    String apiUrl;

    apiUrl = AppConfig.protocolHttp + AppConfig.ZAPPAPP_API_BASE_URL;

    Logger.d("Base url API [ " + apiUrl + " ]");

    Retrofit restAdapter = new Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(apiUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();

    return restAdapter.create(ZappAppApis.class);
}

这就是我提供OkHttpClient的方式:

@Provides
OkHttpClient okHttpClient() {
    final OkHttpClient client = new OkHttpClient();

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();

    if (BuildConfig.DEBUG)
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    else
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);

    // set disk cache
    client.setCache(new Cache(context.getCacheDir(), DISK_CACHE_SIZE));

    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.add(loggingInterceptor);
    interceptors.add(new LoggingInterceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();

            Request request = original.newBuilder()
                    .header("Key", AppConfig.ZAPPAPP_CLIENT_KEY)
                    .header("Cache-Control", "public, max-age=60")
                    .method(original.method(), original.body())
                    .build();

            return chain.proceed(request);
        }
    });

    client.networkInterceptors().addAll(interceptors);

    return client;
}

1 个答案:

答案 0 :(得分:0)

脱机时的数据来自HTTP缓存。

您在这里创建缓存:

// set disk cache
client.setCache(new Cache(context.getCacheDir(), DISK_CACHE_SIZE));

在这里,您要为每个请求添加一个缓存标头:

interceptors.add(new LoggingInterceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();

        Request request = original.newBuilder()
                .header("Key", AppConfig.ZAPPAPP_CLIENT_KEY)
                .header("Cache-Control", "public, max-age=60")
                .method(original.method(), original.body())
                .build();

        return chain.proceed(request);
    }
});

如果缓存不是来自服务器,则可以删除.header("Cache-Control", "public, max-age=60")。如果缓存来自服务器,您可以使用.header("Cache-Control", "max-stale=0")代替它,它应该摆脱缓存。