Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve FATAL EXCEPTION while calling web request from retrofit2?

I am trying to call a GET request to an API but retrofit throws a FATAL EXCEPTION

Error:

2019-12-18 22:26:55.733 27892-29449/com.shashank.foe E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.shashank.foe, PID: 27892
    java.lang.BootstrapMethodError: Exception from call site #1 bootstrap method
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.onResponse(DefaultCallAdapterFactory.java:76)
        at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:129)
        at okhttp3.RealCall$AsyncCall.run(RealCall.kt:138)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.NoClassDefFoundError: Invalid descriptor: VLLLLLZ.
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.onResponse(DefaultCallAdapterFactory.java:76) 
        at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:129) 
        at okhttp3.RealCall$AsyncCall.run(RealCall.kt:138) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
        at java.lang.Thread.run(Thread.java:764) 

Here's my code:

MainActivity.kt

val retrofitService = RetrofitService()
val userApi = retrofitService.createService(UserApi::class.java)

val call = userApi.get()

call.enqueue(object : Callback<Temp> {
   override fun onResponse(call: Call<Temp>,response: Response<Temp>) {
      Log.d(TAG, response.body()!!.id.toString())
   }

   override fun onFailure(call: Call<Temp>,t: Throwable) {
      Log.e(TAG, t.message, t)
   }
})

UserApi.kt

interface UserApi {
    @GET("todos/1")
    fun get():Call<Temp>
}

Temp.kt

data class Temp (
    @SerializedName("userId") val userId : Int,
    @SerializedName("id") val id : Int,
    @SerializedName("title") val title : String,
    @SerializedName("completed") val completed : Boolean
)

RetrofitService.kt

class RetrofitService {

    private val BASE_URL = "https://jsonplaceholder.typicode.com/"


    private val loggingInterceptor = HttpLoggingInterceptor()


    private val httpClient = OkHttpClient.Builder().addInterceptor(loggingInterceptor).build()

    private val builder = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(httpClient)

    private val retrofit = builder.build()

    init {
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
    }

    fun <S> createService(
        serviceClass: Class<S>
    ): S {
        return retrofit.create(serviceClass)
    }
}

build.gradle:

implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:retrofit:2.7.0'
implementation 'com.squareup.retrofit2:converter-gson:2.7.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'
implementation 'org.conscrypt:conscrypt-android:2.2.1'

I have tried searching online to resolve the issue but wasn't able to find any solution.

Any help will be appreciated.

like image 943
Shashank Mishra Avatar asked Dec 13 '22 09:12

Shashank Mishra


1 Answers

Retrofit 2 uses OKhttp3. In that case you need to add

android {
    compileOptions {
        targetCompatibility = "8"
        sourceCompatibility = "8"
    }
}

in your application build.gradle.

like image 113
Dinesh Avatar answered Dec 17 '22 23:12

Dinesh