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

蹲街弑〆低调 提交于 2019-12-03 14:53:23

问题


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?


回答1:


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.




回答2:


Get response from API using rxjava use following code

Create class name API

public class Api {

private static final String BASE_URL="https://your_url";

private static Api instance;
private final IApiCall iApiCallInterface;

private Api() {
    Gson gson = new GsonBuilder().setLenient().create();
    final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .build();
    Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).baseUrl(BASE_URL)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson)).build();

    iApiCallInterface = retrofit.create(IApiCall.class);
}

public static Api start() {
    return instance = instance == null ? new Api() : instance;
}

public Observable<Example> getSendMoneyCountries() {
    return iApiCallInterface.getCategoryList();
}
}

Crete Interface name IApiCall here you can make your all othe API requests

public interface IApiCall {
  //response in in json array
  @Headers("Content-Type: application/json")
  @GET("/json")
   Observable<Example> getCategoryList();
}

Write below code in your MainActivity.java

    private static Api api;
    api = Api.start();
    api.getSendMoneyCountries()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new DisposableObserver<Example>() {
                @Override
                public void onNext(Example response) {
                    //Handle logic
                    try {
                        populateCountryList(response);
                    }catch (Exception e)
                    {
                        finish();
                        Toast.makeText(MainActivity.this,"Unable to send money",Toast.LENGTH_SHORT).show();
                        //MainActivity.showTimeoutDialog();
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(Throwable e) {
                    //Handle error
                    Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onComplete() {
                }
            });


来源:https://stackoverflow.com/questions/39016080/how-to-get-the-request-url-in-retrofit-2-0-with-rxjava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!