1

I need to try to get the full URL Retrofit is using for make API calls.

For example:

@GET("recipes/hot/") suspend fun doCall(): Recipes 

I want to somehow get the full URL (base url + paths etc.) that this API call is doing.

One solution I've found was:

@GET("recipes/hot/") suspend fun doCall(): Response<Recipes> 

Then you can get the url from this response wrapper class.

However, the rest of the api calls in the codebase doesn't wrap the return type with Response, so I really want to avoid doing this.

Is there some easy way I can get the full URL from a Retrofit api call?

1

1 Answer 1

2

You need to add interceptor logging on retrofit for debug mode which will provide you logs in Android Studio logcat while API hit and return response as well. Below is the example, Refactor it as per your need.

HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); OkHttpClient.Builder httpclient = new OkHttpClient.Builder(); httpclient.addInterceptor(logging); if (BuildConfig.DEBUG) { // development build logging.setLevel(HttpLoggingInterceptor.Level.BODY); } else { // production build logging.setLevel(HttpLoggingInterceptor.Level.BASIC); } Retrofit retrofit = new Retrofit.Builder() .baseUrl(WebServiceConstants.SERVER_URL) .client(httpclient.build()) .addConverterFactory(GsonConverterFactory.create()) .build(); 
Sign up to request clarification or add additional context in comments.

2 Comments

I don't need logging of calls for debugging purposes. But I just need the full URL to pass to another function call whenever an API call is made. Would the HttpLoggingInterceptor help with this, or does it only output the info into logcat?
Yes yo can get your full api url from logging. And for debug only means it will help you while development not for release.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.