Override 2 check call url

Is it possible to compare Call URLs with String in Retrofit 2 ?

For example, we can take this baseUrl :

 https://www.google.com 

And this Call :

 public interface ExampleService { @GET("dummy/{examplePartialUrl}/") Call<JsonObject> exampleList(@Path("examplePartialUrl") String examplePartialUrl; } 

with this request:

 Call<JsonObject> mCall = dummyService.exampleList("partialDummy") 

Is there a way to get https://www.google.com/dummy/partialDummy or also dummy/partialDummy before receiving a response from the call?

+8
source share
3 answers

Assuming you use OkHttp along with Retrofit, you can do something like:

dummyService.exampleList("partialDummy").request().url().toString()

which according to the docs OkHttp should print:

https://www.google.com/dummy/partialDummy

+21
source

Personally, I found another way to do this using upgrade 2 and RxJava

First you need to create an OkHttpClient object

 private OkHttpClient provideOkHttpClient() { //this is the part where you will see all the logs of retrofit requests //and responses HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); return new OkHttpClient().newBuilder() .connectTimeout(500, TimeUnit.MILLISECONDS) .readTimeout(500,TimeUnit.MILLISECONDS) .addInterceptor(logging) .build(); } 

after creating this object, the next step is to simply use it in the modified constructor

 public Retrofit provideRetrofit(OkHttpClient client, GsonConverterFactory convertorFactory,RxJava2CallAdapterFactory adapterFactory) { return new Retrofit.Builder() .baseUrl(mBaseUrl) .addConverterFactory(convertorFactory) .addCallAdapterFactory(adapterFactory) .client(client) .build(); } 

one of the attributes that you can assign to Retrofit Builder is the client, install the client for the client from the first function.

After running this code, you can find the OkHttp tag in logcat and you will see the requests and answers you made.

+1
source
 Log.d(TAG, "onResponse: ConfigurationListener::"+call.request().url()); 
+1
source

All Articles