通过RXJava和翻新获取标题信息 [英] Getting Header information with RXJava and Retrofit

查看:71
本文介绍了通过RXJava和翻新获取标题信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将当前使用Retrofit的应用程序转换为RX Java.为了处理分页,传统上我是从响应头中获取nextPage URL.

I'm trying to convert my app which currently uses Retrofit, to use RX Java. In order to handle Pagination, I traditionally was grabbing the nextPage URL from the response headers.

@Override
    public void success(Assignment assignment, Response response) {
        response.getHeaders(); // Do stuff with header info
}

但是,由于切换到RX Java,所以我不确定如何从改造调用中获取响应信息.

However, since switching to RX Java, i'm not sure how to get the response information from my retrofit call.

   @GET("/{item_id}/users")
    Observable<List<Objects>> getObjects(@Path("object_id") long object_id);

    @GET("/{next}")
    Observable<List<Objects>> getNextPageObjects(@Path("next") String nextURL);

有没有办法让我的改造电话返回我的标头信息以及我的Typed对象?

Is there a way to have my retrofit calls to return my header information along with my Typed objects?

推荐答案

您可以使用

Observable<Response>

作为返回类型以获取响应详细信息

as return type to get the response details

@GET("/{item_id}/users")
Observable<Response> getObjects(@Path("object_id") long object_id);

@GET("/{next}")
Observable<Response>getNextPageObjects(@Path("next") String nextURL);

这是Response对象的外观

This is how the Response object would look like

然后您必须从可观察到的

You would have to then parse the headers and body from the observable

serviceClass.getNextPageObjects("next").flatMap(new Func1<Response, Observable<List<Objects>>() {
    @Override
    public Observable<AuthState> call(Response response) {
         List<Header> headers = response.getHeaders();
         GsonConverter converter = new GsonConverter(new Gson());
         // you would have to change this to convert the objects to list
         List<Objects> list = converter.fromBody(response.getBody(),
                            YourClass.class);

        return Observable.from(list);
    }

}

这篇关于通过RXJava和翻新获取标题信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆