Android 如何解决从2调用web请求时出现的致命异常?
我试图调用一个API的GET请求,但改型会引发一个Android 如何解决从2调用web请求时出现的致命异常?,android,rest,api,kotlin,retrofit2,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 boot
致命异常
错误:
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)
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'
这是我的密码:
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)
}
})
interface UserApi {
@GET("todos/1")
fun get():Call<Temp>
}
data class Temp (
@SerializedName("userId") val userId : Int,
@SerializedName("id") val id : Int,
@SerializedName("title") val title : String,
@SerializedName("completed") val completed : Boolean
)
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)
}
}
改装服务.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)
}
})
interface UserApi {
@GET("todos/1")
fun get():Call<Temp>
}
data class Temp (
@SerializedName("userId") val userId : Int,
@SerializedName("id") val id : Int,
@SerializedName("title") val title : String,
@SerializedName("completed") val completed : Boolean
)
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)
}
}
我曾尝试在线搜索以解决此问题,但未能找到任何解决方案
任何帮助都将不胜感激。改装2使用OKhttp3。
在这种情况下,您需要添加
android {
compileOptions {
targetCompatibility = "8"
sourceCompatibility = "8"
}
}
在您的应用程序build.gradle.中,只需将此添加到您的
build.gradle(应用程序)
:
我相信这4行代码可以解决我2天来的头痛问题。谢谢@H.Fa8