如何使用rxjava在Retrofit 2.0中获取请求url? [英] How to get the request url in retrofit 2.0 with rxjava?

查看:179
本文介绍了如何使用rxjava在Retrofit 2.0中获取请求url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试升级到Retrofit 2.0,并在我的android项目中添加RxJava.我正在进行api调用,并希望使用sqlite中的响应数据作为缓存来检索url及其它

I'm trying to upgrade to Retrofit 2.0 and add RxJava in my android project. I'm making an api call and want to retrieve the url and it with the response data in sqlite as a cache

Observable<MyResponseObject> apiCall(@Body body);

在RxJava调用中:

And in the RxJava call:

myRetrofitObject.apiCall(body).subscribe(new Subscriber<MyResponseObject>() {
    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onNext(MyResponseObject myResponseObject) {

    }
});

在Retrofit 1.9中,我们可以在成功回调中获取该网址:

In Retrofit 1.9, we could get the url in the success callback:

        @Override
        public void success(MyResponseObject object, Response response) {
            String url=response.getUrl();
            //save object data and url to sqlite
        }

如何使用RxJava使用Retrofit 2.0?

How do you do this with Retrofit 2.0 using RxJava?

推荐答案

更新:

再次阅读问题后:

如果要访问原始响应,则需要将API接口定义为:

If you want access to the raw response you need to define your API interface as:

Observable<Response<MyResponseObject>> apiCall(@Body body);

代替:

Observable<MyResponseObject> apiCall(@Body body);

您可以使用以下网址获取网址:

You can get the Url using:

response.raw().request().url()

此处:
response 是Retrofit
的响应 raw 是OkHttp
的响应 request 是OkHttp的请求,其中包含 Url 作为 HttpUrl .

here:
response is the response from Retrofit
raw is the response from OkHttp
request is the Request from OkHttp which contains the Url as HttpUrl.

这篇关于如何使用rxjava在Retrofit 2.0中获取请求url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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