Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2 check call URL

Is there any possibility to compare a Call URL with a 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")

There's a way to obtain https://www.google.com/dummy/partialDummy or also dummy/partialDummy before getting the response from the call?

like image 953
Giorgio Antonioli Avatar asked Apr 10 '16 16:04

Giorgio Antonioli


3 Answers

Assuming you're using OkHttp alongside Retrofit, you could do something like:

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

which according to the OkHttp docs should print:

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

like image 88
telkins Avatar answered Nov 04 '22 22:11

telkins


Log.d(TAG, "onResponse: ConfigurationListener::"+call.request().url());
like image 37
Sunil Kumar Avatar answered Nov 04 '22 23:11

Sunil Kumar


Personally I found another way to accomplish this by using retrofit 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 just use it in retrofit builder

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 you can assign to Retrofit builder is the client, set the client to the client from the first function.

After running this code you could search for OkHttp tag in the logcat and you will see the requests and responses you made.

like image 1
Anton Makov Avatar answered Nov 04 '22 22:11

Anton Makov