gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:41:33 25 4
gpt4 key购买 nike

我尝试向 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com