英文:
FATAL EXCEPTION: OkHttp Dispatcher when trying to retrieve data from API using Retrofit
问题
我已经查阅了一些 Stack Overflow 的解决方案,大部分建议启用 Java 8,但对我来说仍然不起作用。我应用的最低 SDK 版本是 26。完整的错误代码如下:
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.myRestApp, PID: 28626
java.lang.BootstrapMethodError: Exception from call site #2 bootstrap method
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.onFailure(DefaultCallAdapterFactory.java:87)
at retrofit2.OkHttpCall$1.callFailure(OkHttpCall.java:142)
at retrofit2.OkHttpCall$1.onFailure(OkHttpCall.java:137)
at okhttp3.RealCall$AsyncCall.run(RealCall.kt:153)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.onFailure(DefaultCallAdapterFactory.java:87)
at retrofit2.OkHttpCall$1.callFailure(OkHttpCall.java:142)
at retrofit2.OkHttpCall$1.onFailure(OkHttpCall.java:137)
at okhttp3.RealCall$AsyncCall.run(RealCall.kt:153)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
我已经在我的 build.gradle 文件中启用了 Java 8。我的 Java 类如下所示:
private void getPhotos() {
ApiInterfrace apiInterfrace = ServiceGenerator.createService(ApiInterfrace.class);
Call<List<Photo>> photosList = apiInterfrace.getPhotos();
photosList.enqueue(new Callback<List<Photo>>() {
@Override
public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
if (response.isSuccessful()) {
for (Photo photo : response.body()) {
photos.add(photo);
// Log.d("TAG", photo.getUrl().getFull());
}
photosAdatper.notifyDataSetChanged();
} else {
Log.d(TAG, "Fail");
}
showProgressbar(false);
}
@Override
public void onFailure(Call<List<Photo>> call, Throwable t) {
Log.d(TAG, "fail" + t.getMessage());
}
});
}
英文:
I have gone through a couple of Stack Overflow solutions that mostly suggested enabling Java 8, but it is still not working for me. Minimum SDK version for my app is 26. The complete error code:
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.myRestApp, PID: 28626
java.lang.BootstrapMethodError: Exception from call site #2 bootstrap method
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.onFailure(DefaultCallAdapterFactory.java:87)
at retrofit2.OkHttpCall$1.callFailure(OkHttpCall.java:142)
at retrofit2.OkHttpCall$1.onFailure(OkHttpCall.java:137)
at okhttp3.RealCall$AsyncCall.run(RealCall.kt:153)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.onFailure(DefaultCallAdapterFactory.java:87) 
at retrofit2.OkHttpCall$1.callFailure(OkHttpCall.java:142) 
at retrofit2.OkHttpCall$1.onFailure(OkHttpCall.java:137) 
at okhttp3.RealCall$AsyncCall.run(RealCall.kt:153) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
at java.lang.Thread.run(Thread.java:764) 
I have already enabled Java 8 in my build.gradle file. My Java class is as below:
private void getPhotos() {
ApiInterfrace apiInterfrace = ServiceGenerator.createService(ApiInterfrace.class);
Call<List<Photo>> photosList = apiInterfrace.getPhotos();
photosList.enqueue(new Callback<List<Photo>>() {
@Override
public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
if (response.isSuccessful()) {
for (Photo photo : response.body()) {
photos.add(photo);
// Log.d("TAG", photo.getUrl().getFull());
}
photosAdatper.notifyDataSetChanged();
} else {
Log.d(TAG, "Fail");
}
showProgressbar(false);
}
@Override
public void onFailure(Call<List<Photo>> call, Throwable t) {
Log.d(TAG, "fail" + t.getMessage());
}
});
}
答案1
得分: 3
尝试将您的Gradle版本更改为1.8,而不是8。
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
英文:
Try change your gradle to version 1.8 instead of 8
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论