如何登录,URL 我正在访问改造
How to Log, the URL I am accessing retrofit
我正在使用改造与 BE 通信,我想记录我正在点击的 URL 和我正在发送的 body 以及我收到的响应(例如 401、404 ).
这是为了知道我是否打对了url什么是header,body和实际的URL来验证我发送的是否正确所需信息
我实现了 HttpLoggingInterceptor 但它没有记录它,或者我以正确的方式记录它
你能推荐一下吗
object RetrofitBuilder {
private const val BASE_URL = "https://xxxx.xxxxxx.com/"
private fun getRetrofit(): Retrofit {
var interceptor: HttpLoggingInterceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build() //Doesn't require the adapter
}
val apiService: ApiService = getRetrofit().create(ApiService::class.java)
}
应用服务
@POST
suspend fun postData(@Url url: String, @Body Data: Data, @Header("Autzn") authHeader: String)
ApiHelper
//注意:这里我使用不同的 URL 作为但不是顶部定义的 baseUrl
apiService.postData("https://abcd.com/msg/oauth2/123456", Data,
"token")
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'
请建议如何记录 url 信息
谢谢
R
这就是我在日志中记录 url 的方式
val retrofitApi: RetrofitInterface
get() {
if (retrofitInterface == null) {
val logger = HttpLoggingInterceptor()
logger.level = HttpLoggingInterceptor.Level.BODY
val okHttpClient = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(logger).build()
val retrofit = Retrofit.Builder()
.baseUrl(REST_HOST)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder()
.setLenient()
.create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
retrofitInterface = retrofit.create(RetrofitInterface::class.java)
}
return this.retrofitInterface!!
}
直接给HttpLoggingInterceptor.Logger接口
日志拦截器应该是这样的
val loggingInterceptor = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
override fun log(message: String) {
Timber.tag("retrofit").d(message)
}
})
我建议您使用 HttpLoggingInterceptor
和 日志级别 ,日志记录可能是一项繁重的任务,尤其是图像(打印原始字节)和大图像 (40mb+)可以抛出OOM异常。
所以使用日志记录进行调试。
public HttpLoggingInterceptor loggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor
= new HttpLoggingInterceptor(message -> Log.d(TAG, message));
loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
return loggingInterceptor;
}
我正在使用改造与 BE 通信,我想记录我正在点击的 URL 和我正在发送的 body 以及我收到的响应(例如 401、404 ).
这是为了知道我是否打对了url什么是header,body和实际的URL来验证我发送的是否正确所需信息
我实现了 HttpLoggingInterceptor 但它没有记录它,或者我以正确的方式记录它
你能推荐一下吗
object RetrofitBuilder {
private const val BASE_URL = "https://xxxx.xxxxxx.com/"
private fun getRetrofit(): Retrofit {
var interceptor: HttpLoggingInterceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build() //Doesn't require the adapter
}
val apiService: ApiService = getRetrofit().create(ApiService::class.java)
}
应用服务
@POST
suspend fun postData(@Url url: String, @Body Data: Data, @Header("Autzn") authHeader: String)
ApiHelper
//注意:这里我使用不同的 URL 作为但不是顶部定义的 baseUrl
apiService.postData("https://abcd.com/msg/oauth2/123456", Data,
"token")
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'
请建议如何记录 url 信息
谢谢 R
这就是我在日志中记录 url 的方式
val retrofitApi: RetrofitInterface
get() {
if (retrofitInterface == null) {
val logger = HttpLoggingInterceptor()
logger.level = HttpLoggingInterceptor.Level.BODY
val okHttpClient = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(logger).build()
val retrofit = Retrofit.Builder()
.baseUrl(REST_HOST)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder()
.setLenient()
.create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
retrofitInterface = retrofit.create(RetrofitInterface::class.java)
}
return this.retrofitInterface!!
}
直接给HttpLoggingInterceptor.Logger接口
日志拦截器应该是这样的
val loggingInterceptor = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
override fun log(message: String) {
Timber.tag("retrofit").d(message)
}
})
我建议您使用 HttpLoggingInterceptor
和 日志级别 ,日志记录可能是一项繁重的任务,尤其是图像(打印原始字节)和大图像 (40mb+)可以抛出OOM异常。
所以使用日志记录进行调试。
public HttpLoggingInterceptor loggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor
= new HttpLoggingInterceptor(message -> Log.d(TAG, message));
loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
return loggingInterceptor;
}