Retrofit 2 check call URL

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 18:17:28

问题


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?


回答1:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/36532273/retrofit-2-check-call-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!