android - 从 Retrofit2 调用 Web 请求时如何解决致命异常?

标签 android rest api kotlin retrofit2

我尝试向 API 调用 GET 请求,但改造抛出致命异常

错误:

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/

相关文章:

android - android中的两个应用程序可以使用本地套接字进行通信吗?

java - 为什么要使用 REST @Produces 注解

rest - HTTP 方法 - POST 与 PATCH 或 PUT - 当用户实际上不打算更新任何内容时

api - Youtube api获取最新的上传缩略图

python - 如何使用 python discord api 中的 add_roles 方法

android - 在没有 Youtube API 的情况下在 Android 应用程序中播放视频

java - android 我的应用程序还在运行吗?

java - 第一个 Android 应用程序中的错误

c# - 如何从控制台应用程序调用 REST API?

delphi - Delphi2009 的 PC-SC 包装器或 Omnikey Sync API 示例