首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Android webview中处理cookies

在Android webview中处理cookies
EN

Stack Overflow用户
提问于 2011-03-08 21:01:26
回答 1查看 4.3K关注 0票数 0

我写了下面的html文件

代码语言:javascript
运行
复制
<code>
<html>
    <head>
        <script text ="text/javascript">
            function createCookie(name,value,days) {
                if (days) {
                    var date = new Date();
                    date.setTime(date.getTime()+(days*24*60*60*1000));
                    var expires = "; expires="+date.toGMTString();
                }
                else var expires = "";
                document.cookie = name+"="+value+expires+"; path=/";
            }

            function readCookie(name) {
                var nameEQ = name + "=";
                var ca = document.cookie.split(';');
                for(var i=0;i < ca.length;i++) {
                    var c = ca[i];
                    while (c.charAt(0)==' ') c = c.substring(1,c.length);
                    if (c.indexOf(nameEQ) == 0) return(c.substring(nameEQ.length,c.length));
                }
                return null;
            }

            function modifyCookie() {
                var newsessid = document.getElementById('userid').value;
                //var oldsessid = document.getElementById('userid').value;
                if(newsessid != "") {
                        eraseCookie('session id') ;
                        createCookie('session id', newsessid, 1);
                        //alert(readCookie('session id'));
                        document.getElementById('cookies').innerHTML = readCookie('session id');
                        return true;
                }

                //.value = readCookie('session id');
            }

            function eraseCookie(name) {
                createCookie(name,"",-1);
            }

            function fillTextBox() {
                //alert("Running");
                document.getElementById('cookies').innerHTML = readCookie('session id');
                document.getElementById('userId').value = document.getElementById('sessid').value;
            }
        </script>
    <head>
    <body onload="fillTextBox()" style = "height : 480px; width : 320px; background-image : url('investmentbanking/background2.png'); background-repeat : no-repeat">
        <div style = "position : absolute; left = 80px; top = 160px;">  
            <label style = "color : #ffffff;">Session Id :</label>
            <input type="text" id="userid" name="session"/>
            <br/><br/>
            <input type="hidden" id ="sessid" name="user" value="truce"/>
            <input type="button" onclick="modifyCookie()" value="Modify Cookie" />
            <br/>
            <label>Stored Cookies : <label/><label id = "cookies"/>
        </div>  
    </body>
</html>
</code>

当我在android中用我的webview打开这个文件,修改文本框的内容,点击修改cookie按钮,“cookie”旁边的文本没有改变。代码在桌面的Firefox和IE上运行正常,但在这里不能。问题是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-22 03:32:52

您必须初始化绑定到应用程序的CookieSyncManager实例。下面是我的:

代码语言:javascript
运行
复制
final Context myApp = this;
CookieSyncManager.createInstance(myApp);
CookieSyncManager.getInstance().sync();

只需将其添加到加载webview的活动的OnCreate()方法中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5232810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档