Retrofit 2 check call URL

前端 未结 3 1880
轮回少年
轮回少年 2020-12-16 02:05

Is there any possibility to compare a Call URL with a String in Retrofit 2?

For example we can take this baseUrl:

相关标签:
3条回答
  • 2020-12-16 02:24

    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

    0 讨论(0)
  • 2020-12-16 02:32
    Log.d(TAG, "onResponse: ConfigurationListener::"+call.request().url());
    
    0 讨论(0)
  • 2020-12-16 02:39

    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.

    0 讨论(0)
提交回复
热议问题