How can I debug my retrofit API call?

后端 未结 3 1492
南笙
南笙 2021-02-06 23:01

I\'m using retrofit to get some data from the Flickr api. The method I\'m making the call in looks like this:

public static List getImageIds(int si         


        
3条回答
  •  礼貌的吻别
    2021-02-06 23:38

    You can use the following class to log API calls

    import okhttp3.OkHttpClient
    import okhttp3.logging.HttpLoggingInterceptor
    
    object HTTPLogger {
        fun getLogger(): OkHttpClient {
            /*
             * OKHTTP interceptor to log all API calls
             */
            val interceptor = HttpLoggingInterceptor()
            interceptor.level = HttpLoggingInterceptor.Level.BODY
            val client = OkHttpClient.Builder()
                    .addInterceptor(interceptor)
                    .build()
            return client
        }
    }
    

    You can then call this class in your retrofit instance class like this

    import retrofit2.Retrofit
    import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
    import retrofit2.converter.gson.GsonConverterFactory
    
    object RetrofitClientInstance {
        private var retrofit: Retrofit? = null
        val retrofitInstance: Retrofit?
            get() {
                if (retrofit == null) {
                    retrofit = Retrofit.Builder()
                            .baseUrl(Constants.BASE_URL)
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .addConverterFactory(GsonConverterFactory.create())
                            .client(HTTPLogger.getLogger())
                            .build()
                }
                return retrofit
            }
    }
    

    The dependency required is

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

提交回复
热议问题