Warm tip: This article is reproduced from stackoverflow.com, please click
android rx-java retrofit2

How to get the request url in retrofit 2.0 with rxjava?

发布于 2020-04-03 23:20:26

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);

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) {

    }
});

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
        }

How do you do this with Retrofit 2.0 using RxJava?

Questioner
何福毅
Viewed
196
LordRaydenMK 2016-08-21 16:05

Update:

After reading the question again:

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

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

instead of:

Observable<MyResponseObject> apiCall(@Body body);

You can get the Url using:

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

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.