아래와 같이 Retrofit builder에 interceptor를 넣어서 디버깅하면 편리 하다.
아래 주석을 한 것처럼 원하는 데이터만 출력 하는 방법이 있는가 하면,
HttpLoggingInterceptor 객체를 생성하여 level만 정해 주고 그냥 쓰는 방법이 있다.
HttpLoggingInterceptor로 왠만한 정보를 볼 수 있다.
interface GithubApi {
@GET
fun getUserList(@Url url: String) : Observable<UserResponse>
}
object GithubApiProvider {
fun provideGithubApi(): GithubApi {
return Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.client(provideOkHttpClient(provideLoggingInterceptor()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(GithubApi::class.java)
}
private fun provideOkHttpClient(interceptor: HttpLoggingInterceptor): OkHttpClient {
var builder = OkHttpClient.Builder()
builder.addInterceptor(interceptor) // Application Interceptors : Application Interceptor를 등록하려면 OkHttpClient.Builder에서 addInterceptor()를 호출해야 합니다.
return builder.build()
}
private fun provideLoggingInterceptor(): HttpLoggingInterceptor {
var interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
return interceptor
}
//
// private fun provideOkHttpClient(interceptor: Interceptor) : OkHttpClient{
// var builder = OkHttpClient.Builder()
// builder.addInterceptor(interceptor)
//
// return builder.build()
// }
//
// private fun provideLoggingInterceptor(): Interceptor{
//
// return object : Interceptor{
// val TAG = javaClass.simpleName
// override fun intercept(chain: Interceptor.Chain): Response {
// var request = chain.request()
// Log.d(TAG, "request.method" + request.method())
//
// var response = chain.proceed(request)
// Log.d(TAG, "response.url" + response.request().url())
//
// return response
// }
// }
// }
}
... {
...
private var api = GithubApiProvider.provideGithubApi()
disposable.add(api.getUserList(Constant.BASE_URL + Constant.RANDOM_USER_URL)!!
...
}
+
또 다른 샘플 코드
val okHttpClient = OkHttpClient.Builder()
.addInterceptor { chain: Interceptor.Chain ->
val original = chain.request()
chain.proceed(original.newBuilder().apply {
addHeader("X-Naver-Client-Id", Naver_Client_Id)
addHeader("X-Naver-Client-Secret", Naver_Client_Secret)
}.build())
}
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}).build()
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
출처 :
hwanine.github.io/android/Retrofit-OkHttp/
jongmin92.github.io/2018/01/29/Programming/android-retrofit2-okhttp3/