Android 在WebView中存储和使用Cookie

Android 在WebView中存储和使用Cookie,android,cookies,Android,Cookies,嗨,我不知道我在问什么,因为我以前没有做过饼干。在我的应用程序中,我打开了一个WebView,我们提交了一些数据,我想存储cookie,这样当我关闭WebView(也关闭应用程序)并重新打开时,它会使用存储的cookie 我不太确定我的代码目前在做什么,因为我一直在遵循示例 @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { ProgressBar

嗨,我不知道我在问什么,因为我以前没有做过饼干。在我的应用程序中,我打开了一个WebView,我们提交了一些数据,我想存储cookie,这样当我关闭WebView(也关闭应用程序)并重新打开时,它会使用存储的cookie

我不太确定我的代码目前在做什么,因为我一直在遵循示例

@Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            ProgressBar progressBar = (ProgressBar) 
findViewById(R.id.progress_bar);
            progressBar.setIndeterminate(true);
            progressBar.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
            String cookieStr = CookieManager.getInstance().getCookie(url);
            try {
                storeCookies(url, cookieStr);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }


        }

        private void storeCookies(String url, String cookieString) throws URISyntaxException {
            if (cookieString !=null &&  !cookieString.isEmpty()) {
                URI uri = new URI(url);
                List<HttpCookie> cookies = HttpCookie.parse(cookieString);
                for (HttpCookie cookie : cookies) {
                    cookieStore = new CookieStore() {
                        @Override
                        public void add(URI uri, HttpCookie cookie) {
                            cookieStore.add(uri, cookie);
                        }


                        @Override
                        public List<HttpCookie> get(URI uri) {
                            return null;
                        }

                        @Override
                        public List<HttpCookie> getCookies() {
                            return null;
                        }

                        @Override
                        public List<URI> getURIs() {
                            return null;
                        }

                        @Override
                        public boolean remove(URI uri, HttpCookie cookie) {
                            return false;
                        }

                        @Override
                        public boolean removeAll() {
                            return false;
                        }
                    };

                }
            }
        }

        public void onPageFinished(WebView view, String url) {
            ProgressBar progressBar = (ProgressBar)findViewById(R.id.progress_bar);
            progressBar.setVisibility(View.GONE);
            setURL(url);

        }
    });
}
@覆盖
public void onPageStarted(WebView视图、字符串url、位图favicon){
ProgressBar进度条=(ProgressBar)
findViewById(R.id.progress\u bar);
progressBar.SetUndeterminate(真);
progressBar.setVisibility(View.VISIBLE);
super.onPageStarted(视图、url、favicon);
字符串cookieStr=CookieManager.getInstance().getCookie(url);
试一试{
storeCookies(url,cookieStr);
}捕获(URISyntaxException e){
e、 printStackTrace();
}
}
私有void storeCookies(字符串url、字符串cookieString)抛出URISyntaxException{
if(cookieString!=null&&!cookieString.isEmpty()){
URI=新的URI(url);
List cookies=HttpCookie.parse(cookieString);
用于(HttpCookie cookie:cookies){
cookieStore=新cookieStore(){
@凌驾
公共void添加(URI、HttpCookie cookie){
添加(uri,cookie);
}
@凌驾
公共列表获取(URI){
返回null;
}
@凌驾
公共列表getCookies(){
返回null;
}
@凌驾
公共列表getURIs(){
返回null;
}
@凌驾
公共布尔删除(URI,HttpCookie cookie){
返回false;
}
@凌驾
公共布尔removeAll(){
返回false;
}
};
}
}
}
公共void onPageFinished(WebView视图,字符串url){
ProgressBar ProgressBar=(ProgressBar)findViewById(R.id.progress\u bar);
progressBar.setVisibility(View.GONE);
setURL(url);
}
});
}

如果要为站点设置cookie,请遵循以下代码

Kotlin:

val cookieManager = CookieManager.getInstance()
cookieManager.setAcceptCookie(true)
cookieManager.setCookie("YOUR_WEBSITE_URL", "key=value")
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
   val cookieSyncManager = CookieSyncManager.createInstance(this)
   cookieSyncManager.sync()
}
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie("YOUR_WEBSITE_URL", "key=value");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
   CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
   cookieSyncManager.sync();
}
val cookieManager=cookieManager.getInstance()
cookieManager.setAcceptCookie(true)
setCookie(“你的网站地址”,“key=value”)
if(Build.VERSION.SDK\u INT
Java:

val cookieManager = CookieManager.getInstance()
cookieManager.setAcceptCookie(true)
cookieManager.setCookie("YOUR_WEBSITE_URL", "key=value")
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
   val cookieSyncManager = CookieSyncManager.createInstance(this)
   cookieSyncManager.sync()
}
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie("YOUR_WEBSITE_URL", "key=value");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
   CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
   cookieSyncManager.sync();
}
CookieManager CookieManager=CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
setCookie(“你的网站地址”,“key=value”);
if(Build.VERSION.SDK\u INT
用您的网站URL替换您的网站\u URL,并将key=value与值配对。希望这有帮助。

转到