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?

SmallGrammer
  • 893
  • 9
  • 19
  • 1
    Does this answer your question? [Logging with Retrofit 2](https://stackoverflow.com/questions/32514410/logging-with-retrofit-2) – Nitish Dec 24 '21 at 05:36

1 Answers1

1

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();
  • 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? – SmallGrammer Dec 25 '21 at 01:15
  • Yes yo can get your full api url from logging. And for debug only means it will help you while development not for release. – Daniyal Ahmed Khan Dec 27 '21 at 05:51