的WebView和Android的饼干 [英] WebView and Cookies on Android

查看:123
本文介绍了的WebView和Android的饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有工作正常,通过定期的浏览器,通过机器人的WebView但是使用时对appspot一个应用程序,它不能设置和读取的cookies。我不是让饼干外这个Web应用程序顺便说一句,一旦URL是由web视图,所有的处理,IDS等去可以呆在那里,我需要的是会话管理的应用程序中。首先屏幕还加载罚款,所以我知道的WebView +服务器交互不破。

I have an application on appspot that works fine through regular browser, however when used through Android WebView, it cannot set and read cookies. I am not trying to get cookies "outside" this web application BTW, once the URL is visited by WebView, all processing, ids, etc. can stay there, all I need is session management inside that application. First screen also loads fine, so I know WebView + server interactivity is not broken.

我看着WebSettings类中,有像setEnableCookies没有呼叫。

I looked at WebSettings class, there was no call like setEnableCookies.

我打开网址是这样的:

public class MyActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
    WebView webview = new WebView(this);
    setContentView(webview);      
    webview.loadUrl([MY URL]);
  }
  .. 
}

任何想法?

Any ideas?

推荐答案

我想通了,这是怎么回事。

I figured out what's going on.

当我通过服务器端的操作(网址访问)加载一个页面,并查看从web视图里面那个动作返回的HTML,即第一个动作/页的web视图内运行。但是,当你点击,在你的web应用程序动作命令,这些操作任何一个环节开始一个新的浏览器。这就是为什么cookie的信息丢失了,因为你的web视图设置第一个cookie信息消失了,我们这里有一个单独的程序。

When I load a page through a server side action (a url visit), and view the html returned from that action inside a Webview, that first action/page runs inside that Webview. However, when you click on any link that are action commands in your web app, these actions start a new browser. That is why cookie info gets lost because the first cookie information you set for Webview is gone, we have a seperate program here.

您必须拦截点击web视图使浏览从来没有离开应用程序,一切都保持相同的web视图里面。

You have to intercept clicks on Webview so that browsing never leaves the app, everything stays inside the same Webview.

  WebView webview = new WebView(this);      
  webview.setWebViewClient(new WebViewClient() {  
      @Override  
      public boolean shouldOverrideUrlLoading(WebView view, String url)  
      {  
        view.loadUrl(url); //this is controversial - see comments and other answers
        return true;  
      }  
    });                 
  setContentView(webview);      
  webview.loadUrl([MY URL]);

这解决了这个问题。

这篇关于的WebView和Android的饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆