Logging with Retrofit 2

前端 未结 21 1668
春和景丽
春和景丽 2020-11-22 07:33

I\'m trying to get the exact JSON that is being sent in the request. Here is my code:

OkHttpClient client = new OkHt         


        
21条回答
  •  庸人自扰
    2020-11-22 08:04

    First Add dependency to build.gradle:

    implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'

    While using Kotlin you can add Logging Interceptor like this :

    companion object {
        val okHttpClient = OkHttpClient().newBuilder()
                .addInterceptor(HttpLoggingInterceptor().apply {
                    level = HttpLoggingInterceptor.Level.BODY
                })
                .build()
    
    
        fun getRetrofitInstance(): Retrofit {
            val retrofit = Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(ScanNShopConstants.BASE_URL)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
    
            return retrofit
        }
    }
    

提交回复
热议问题