严重异常:尝试使用Retrofit从API检索数据时的OkHttp调度程序异常

huangapple go评论83阅读模式
英文:

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)&#160;
        at retrofit2.OkHttpCall$1.callFailure(OkHttpCall.java:142)&#160;
        at retrofit2.OkHttpCall$1.onFailure(OkHttpCall.java:137)&#160;
        at okhttp3.RealCall$AsyncCall.run(RealCall.kt:153)&#160;
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)&#160;
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)&#160;
        at java.lang.Thread.run(Thread.java:764)&#160;

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&lt;List&lt;Photo&gt;&gt; photosList = apiInterfrace.getPhotos();
        photosList.enqueue(new Callback&lt;List&lt;Photo&gt;&gt;() {
            @Override
            public void onResponse(Call&lt;List&lt;Photo&gt;&gt; call, Response&lt;List&lt;Photo&gt;&gt; response) {
                if (response.isSuccessful()) {
                    for (Photo photo : response.body()) {
                        photos.add(photo);
                     //   Log.d(&quot;TAG&quot;, photo.getUrl().getFull());

                    }
                    photosAdatper.notifyDataSetChanged();
                } else {
                    Log.d(TAG, &quot;Fail&quot;);
                }
                showProgressbar(false);
            }

            @Override
            public void onFailure(Call&lt;List&lt;Photo&gt;&gt; call, Throwable t) {
                Log.d(TAG, &quot;fail&quot; + 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
}

huangapple
  • 本文由 发表于 2020年1月30日 18:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59983848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定