创建 OkHttp3.OkHttpClient 的新实例时出现异常
Exception when creating new instance of OkHttp3.OkHttpClient
我正在尝试使用 OkHttp 从 Android 应用中的网络服务器下载文件。在使用 Android Studio(API 级别 28)创建新项目后,我添加了一个按钮并将 ImageView 添加到我的 Activity。我的想法是在我按下按钮时下载图像并将其显示在屏幕上。
我使用的是最新版本的 OkHttp,3.14.0。
我在 AndroidManifest.xml 文件中添加了 <uses-permission android:name="android.permission.INTERNET"/>
。
我尝试完全按照 examples 中的描述使用 OkHttpClient。这是我的代码的样子:
public class MainActivity extends AppCompatActivity {
... // onCreate method
public void btnClicked(View view) {
new DownloadImageAsyncTask().execute("https://link.to/image.jpg");
}
public class DownloadImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
OkHttpClient client = new OkHttpClient();
... // override doInBackground()
}
}
当 l 运行 应用程序并按下按钮时,执行 btnClicked()
方法并且应用程序崩溃。调用 OkHttpClient()
时发生错误,无论我将其作为 DownloadImageAsyncTask
class 中的字段,作为 doInBackground()
方法的局部变量还是作为 doInBackground()
方法中的字段ManActivity
class.
我也尝试过使用 Builder (new OkHttpClient.Builder().build()
) 来初始化对象,但结果相同。
这是堆栈跟踪:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.httpstuff, PID: 17796
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access00(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access00(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method
at okhttp3.internal.Util.<clinit>(Util.java:87)
at okhttp3.internal.Util.immutableList(Util.java:234)
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124)
at com.myapp.httpstuff.MainActivity$DownloadImageAsyncTask.<init>(MainActivity.java:31)
at com.myapp.httpstuff.MainActivity.btnClicked(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access00(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at okhttp3.internal.Util.<clinit>(Util.java:87)
at okhttp3.internal.Util.immutableList(Util.java:234)
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124)
at com.myapp.httpstuff.MainActivity$DownloadImageAsyncTask.<init>(MainActivity.java:31)
at com.myapp.httpstuff.MainActivity.btnClicked(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access00(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
在尝试了我想到的一切之后,我尝试使用旧版本的 OkHttp。
改用 OkHttp:2.7.2
,我的代码按预期工作。
我也刚刚测试了 Okhttp3:3.12.2
,这也解决了这个问题。
有趣的是 OkHttp3:3.12.2
在 OkHttp3:3.14.0
(https://github.com/square/okhttp/releases)
之后的一天发布
OkHttp 3.13+ 需要 Java 8+ 或 Android 5+。我认为您遇到过 ART 运行时不期望 Java 8 lambda 的情况。
Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
if (UNLIKELY(object.IsNull())) {
// This will typically be for LambdaMetafactory which is not supported.
ThrowClassCastException("Bootstrap method returned null");
return nullptr;
}
您可能会成功更新 build.gradle。
android {
compileOptions {
targetCompatibility = "8"
sourceCompatibility = "8"
}
}
这是为什么它与 Okhttp3:3.12.2
一起工作的答案:
Google’s distribution dashboard shows that ~11% of the devices that visited the Play Store in October 2018 were running Android 4.x. We’ve created a branch, OkHttp 3.12.x, to support these devices. Should we encounter any severe bugs or security problems we’ll backport the fixes and release. We plan to maintain this branch through December 31, 2020.
来源:https://medium.com/square-corner-blog/okhttp-3-13-requires-android-5-818bb78d07ce
我正在尝试使用 OkHttp 从 Android 应用中的网络服务器下载文件。在使用 Android Studio(API 级别 28)创建新项目后,我添加了一个按钮并将 ImageView 添加到我的 Activity。我的想法是在我按下按钮时下载图像并将其显示在屏幕上。
我使用的是最新版本的 OkHttp,3.14.0。
我在 AndroidManifest.xml 文件中添加了 <uses-permission android:name="android.permission.INTERNET"/>
。
我尝试完全按照 examples 中的描述使用 OkHttpClient。这是我的代码的样子:
public class MainActivity extends AppCompatActivity {
... // onCreate method
public void btnClicked(View view) {
new DownloadImageAsyncTask().execute("https://link.to/image.jpg");
}
public class DownloadImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
OkHttpClient client = new OkHttpClient();
... // override doInBackground()
}
}
当 l 运行 应用程序并按下按钮时,执行 btnClicked()
方法并且应用程序崩溃。调用 OkHttpClient()
时发生错误,无论我将其作为 DownloadImageAsyncTask
class 中的字段,作为 doInBackground()
方法的局部变量还是作为 doInBackground()
方法中的字段ManActivity
class.
我也尝试过使用 Builder (new OkHttpClient.Builder().build()
) 来初始化对象,但结果相同。
这是堆栈跟踪:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.httpstuff, PID: 17796
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access00(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access00(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method
at okhttp3.internal.Util.<clinit>(Util.java:87)
at okhttp3.internal.Util.immutableList(Util.java:234)
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124)
at com.myapp.httpstuff.MainActivity$DownloadImageAsyncTask.<init>(MainActivity.java:31)
at com.myapp.httpstuff.MainActivity.btnClicked(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access00(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at okhttp3.internal.Util.<clinit>(Util.java:87)
at okhttp3.internal.Util.immutableList(Util.java:234)
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124)
at com.myapp.httpstuff.MainActivity$DownloadImageAsyncTask.<init>(MainActivity.java:31)
at com.myapp.httpstuff.MainActivity.btnClicked(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access00(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
在尝试了我想到的一切之后,我尝试使用旧版本的 OkHttp。
改用 OkHttp:2.7.2
,我的代码按预期工作。
我也刚刚测试了 Okhttp3:3.12.2
,这也解决了这个问题。
有趣的是 OkHttp3:3.12.2
在 OkHttp3:3.14.0
(https://github.com/square/okhttp/releases)
OkHttp 3.13+ 需要 Java 8+ 或 Android 5+。我认为您遇到过 ART 运行时不期望 Java 8 lambda 的情况。
Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
if (UNLIKELY(object.IsNull())) {
// This will typically be for LambdaMetafactory which is not supported.
ThrowClassCastException("Bootstrap method returned null");
return nullptr;
}
您可能会成功更新 build.gradle。
android {
compileOptions {
targetCompatibility = "8"
sourceCompatibility = "8"
}
}
这是为什么它与 Okhttp3:3.12.2
一起工作的答案:
Google’s distribution dashboard shows that ~11% of the devices that visited the Play Store in October 2018 were running Android 4.x. We’ve created a branch, OkHttp 3.12.x, to support these devices. Should we encounter any severe bugs or security problems we’ll backport the fixes and release. We plan to maintain this branch through December 31, 2020.
来源:https://medium.com/square-corner-blog/okhttp-3-13-requires-android-5-818bb78d07ce