android - 将 cookie 从 native 登录传递到 webview

标签 android cookies android-webview httprequest android-cookiemanager

我正在关注 this用于将 cookie 从 native 代码共享到 WebView。我有一个本地登录屏幕。成功登录后,我将 cookie 保存在 CookieSyncManager 中。当 webview 加载时,我将这些 cookie 传递给它,这样登录屏幕就不会出现。
以下是我已经实现的:

public class MyApp extends Application {
    public void onCreate() {
        super.onCreate();

        //Setup Cookie Manager and Persistence to disk
        CookieSyncManager.createInstance(this);
        CookieManager.getInstance().setAcceptCookie(true);
    }
}  

用于登录的 HttpRequest :

private void executeRequest(HttpUriRequest request, String url) {
        DefaultHttpClient client = getDefaultClient(); // new DefaultHttpClient();

        syncCookiesFromAppCookieManager(loginUrl, client);

        HttpResponse httpResponse = null;

        try {
            httpResponse = client.execute(request);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = null;
            try {
            instream = entity.getContent();
              } catch (IOException e1) {
                  e1.printStackTrace();
                }
            response = convertStreamToString(instream);
            response = StringUtils.remove(response, "\n");
            syncCookiesFromAppCookieManager(loginUrl, client);
           // client.setCookieStore((org.apache.http.client.CookieStore) new PersistentCookieStore(context));



}
    }  

WebActivity 中:

CookieSyncManager.getInstance().sync();  
webview.loadUrl(url);  

但我再次在 webView 上获取登录屏幕。即 cookie 不会被存储。

最佳答案

K7Ko's答案终于对我有用。但只有在我评论了这一行之后

cookieManager.removeSessionCookie();

关于android - 将 cookie 从 native 登录传递到 webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29298376/

相关文章:

Python:请求 session 登录 Cookie

c# - Asp.Net Core 3.1 Cookies 未附加 Razor Pages C#

java - OneSignal:收到通知时 Android 应用程序崩溃

android - Recyclerview 保持点击的项目突出显示 - Android

android - 如何在 Android 中重启设备后启动服务

android - 使用 Robotium 4.3 进行网页测试

android - WebView 和 HTML5 <视频>

android - ScrollView 内的可滚动多行 TextView(不涉及触摸交互)

node.js - JWT 未授权错误 : No authorization token was found (GET request with cookie)

java - Android Webview 在 ICS 4.0 + 上无法正确呈现