Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改装2检查呼叫URL_Android_Http_Retrofit_Retrofit2 - Fatal编程技术网

Android 改装2检查呼叫URL

Android 改装2检查呼叫URL,android,http,retrofit,retrofit2,Android,Http,Retrofit,Retrofit2,是否有可能将调用URL与改型2中的字符串进行比较 例如,我们可以使用这个baseUrl: https://www.google.com 而这个呼叫: public interface ExampleService { @GET("dummy/{examplePartialUrl}/") Call<JsonObject> exampleList(@Path("examplePartialUrl") String examplePartialUrl; } 公共接口示例

是否有可能将
调用
URL与
改型2中的
字符串
进行比较

例如,我们可以使用这个
baseUrl

https://www.google.com
而这个
呼叫

public interface ExampleService {
    @GET("dummy/{examplePartialUrl}/")
    Call<JsonObject> exampleList(@Path("examplePartialUrl") String examplePartialUrl;
}
公共接口示例服务{
@获取(“dummy/{examplePartialUrl}/”)
调用exampleList(@Path(“examplePartialUrl”)字符串examplePartialUrl;
}
根据这项请求:

Call<JsonObject> mCall = dummyService.exampleList("partialDummy")
调用mCall=dummyService.exampleList(“partialDummy”)

有一种方法可以获得
https://www.google.com/dummy/partialDummy
dummy/partialDummy
在收到呼叫响应之前?

假设您在改型的同时使用OkHttp,您可以执行以下操作:

dummyService.exampleList(“partialDummy”).request().url().toString()

根据OkHttp文件,应打印以下内容:


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

就我个人而言,我找到了另一种方法,通过使用改型2和RxJava来实现这一点

首先,您需要创建一个OkHttpClient对象

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();
}
创建此对象后,下一步就是在改装生成器中使用它

public Retrofit provideRetrofit(OkHttpClient client, GsonConverterFactory convertorFactory,RxJava2CallAdapterFactory adapterFactory)
{
    return new Retrofit.Builder()
            .baseUrl(mBaseUrl)
            .addConverterFactory(convertorFactory)
            .addCallAdapterFactory(adapterFactory)
            .client(client)
            .build();
}
可以指定给改装生成器的属性之一是客户端,从第一个函数将客户端设置为客户端


运行此代码后,您可以在logcat中搜索OkHttp标记,您将看到您的请求和响应。

非常感谢,我还编辑了添加“.url()的答案只返回字符串格式的url。感谢您的响应。您能解释一下,在使用RxJava时,我们如何实现相同的目标吗?因为在这种情况下,返回类型是可观察的,所以如何获取request()和url?如果我们使用RxJava 2进行改型,会怎么样?
Log.d(TAG, "onResponse: ConfigurationListener::"+call.request().url());