0

我在我的应用程序中使用以下代码在 web 视图中接受 cookie:

CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
CookieManager.setAcceptFileSchemeCookies(true);
CookieManager.getInstance().setAcceptCookie(true);

在 Android 4.x 上运行它运行良好。但我也在使用 2.3.6 的手机上对其进行了测试,并在那里崩溃并出现以下堆栈跟踪:

10-13 22:34:20.519: E/AndroidRuntime(2371): FATAL EXCEPTION: main
10-13 22:34:20.519: E/AndroidRuntime(2371): java.lang.NoSuchMethodError: android.webkit.CookieManager.setAcceptFileSchemeCookies
10-13 22:34:20.519: E/AndroidRuntime(2371):     at de.fblike.facebook.FacebookActivity.onCreate(FacebookActivity.java:28)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at android.os.Looper.loop(Looper.java:130)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at java.lang.reflect.Method.invoke(Method.java:507)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
10-13 22:34:20.519: E/AndroidRuntime(2371):     at dalvik.system.NativeStart.main(Native Method)

所以我问自己:我该如何修复它?好像2.3.6不支持这种方法。

4

2 回答 2

3

我有同样的问题,最终通过这个补丁解决了:

平台/android/CordovaLib/src/org/apache/cordova/engine/SystemCookieManager.java:

         cookieManager = CookieManager.getInstance();

          //REALLY? Nobody has seen this UNTIL NOW?
 -        cookieManager.setAcceptFileSchemeCookies(true);

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
 +            cookieManager.setAcceptFileSchemeCookies(true);
              cookieManager.setAcceptThirdPartyCookies(webView, true);
          }
      }

很痛苦。。

于 2016-09-24T02:17:14.307 回答
1

如果你阅读文档,你会发现这个方法是在 API12 (Android 3.1) 引入的。您必须增加您的 minSdk 版本。

http://developer.android.com/reference/android/webkit/CookieManager.html#setAcceptFileSchemeCookies(boolean)

于 2013-10-13T20:52:17.173 回答