Retrofit 2 check call URL

2020-02-10 06:18发布

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?

3条回答
家丑人穷心不美
2楼-- · 2020-02-10 06:53
Log.d(TAG, "onResponse: ConfigurationListener::"+call.request().url());
查看更多
倾城 Initia
3楼-- · 2020-02-10 07:03

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.

查看更多
小情绪 Triste *
4楼-- · 2020-02-10 07:04

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

查看更多
登录 后发表回答