0

I am using paging library version 2.1.0 in with java. How I can manage false status or empty array from API. I tried finding some solution but didn't get any.

Dheeraj Rijhwani
  • 351
  • 5
  • 18

1 Answers1

0

If you're using Retrofit, you could use Interceptor to intercept error code. Below is the code to handle response code error 401. Similarly, you can handle any response code.

var retrofit:Retrofit = Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(BASE_URL)
        .client(getOkHttpClient())
        .build()

 private fun getOkHttpClient() : OkHttpClient{
        val okHttpCLient = OkHttpClient.Builder()
            .addInterceptor(object : Interceptor{
                override fun intercept(chain: Interceptor.Chain): Response {
                    val request = chain.request()
                    val response = chain.proceed(request)
                    if(response.code() == 401){
                        Log.e(TAG,"Un-Authorized user")
                    }
                    return response
                }
            })
        return okHttpCLient.build()
    }

//OkHttp Library in Gradle file:

implementation 'com.squareup.okhttp3:okhttp:3.12.0'
Rishabh Jain
  • 145
  • 8
  • I am using interceptor in retrofit. But my problem is when we are getting the error or we are getting false from the API how we will show that error in the activity when we are using paging library. – Dheeraj Rijhwani Aug 21 '19 at 11:25
  • if(response.isSuccessful) { } else {// Parse your error : ServiceGenerator.parseError(response)} – Rishabh Jain Aug 21 '19 at 12:01