我正在尝试向 API 调用 GET 请求,但改造会引发 FATAL EXCEPTION
错误 :

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) 

这是我的代码:

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'

我曾尝试在线搜索以解决该问题,但无法找到任何解决方案。

任何帮助将不胜感激。

最佳答案

Retrofit 2 使用 OKhttp3。
在这种情况下,您需要添加

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

在您的应用程序 build.gradle 中。

关于android - 从retrofit2调用Web请求时如何解决致命异常?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59397166/

10-09 06:57