Android 4.3中的Android setcookie和setAcceptCookie方法崩溃

时间:2018-05-14 06:23:13

标签: android android-webview

我需要在我的应用中设置Cookie,因此我在代码中使用了API方法setAcceptCookiesetCookie。当SDK版本> 19,但在SDK版本18中,我的应用程序崩溃了。

以下是我的代码。如果我删除它来调用方法,它运行正常,那么我该怎么做呢?

try {
        final CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        for (final String cookie : cookies) {
            Log.d(">>>>>>> inject Cookie into : " + url  + "  ==> " + cookie);
            cookieManager.setCookie(url, cookie);
        }
    } catch (Exception e) {

    }

Logcat输出:

05-10 16:51:35.351 25280-25336/? I/dalvikvm: "AsyncBackgroundTask #1" prio=5 tid=16 RUNNABLE
05-10 16:51:35.351 25280-25336/? I/dalvikvm:   | group="main" sCount=0 dsCount=0 obj=0x41e2d8d0 self=0x5fb82498
05-10 16:51:35.351 25280-25336/? I/dalvikvm:   | sysTid=25336 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1578780720
05-10 16:51:35.351 25280-25336/? I/dalvikvm:   | state=R schedstat=( 289179937 1233023039 1600 ) utm=23 stm=5 core=3
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at android.webkit.CookieManagerClassic.nativeSetAcceptCookie(Native Method)
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at android.webkit.CookieManagerClassic.setAcceptCookie(CookieManagerClassic.java:44)
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at coj.execute(RealCall.java:69)
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at XXXX
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at xxxxx
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-10 16:51:35.351 25280-25336/? I/dalvikvm:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-10 16:51:35.361 25280-25336/? I/dalvikvm:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-10 16:51:35.361 25280-25336/? I/dalvikvm:     at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:0)

确保使用CookieSyncManager

    CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
    cookieSyncMngr.startSync();
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.setCookie(url, cookie);
    cookieSyncMngr.stopSync();
    cookieSyncMngr.sync();