在Android的web视图和Java HTTP客户端之间的HTTP会话同步视图、客户端、web、Android

2023-09-07 11:01:50 作者:请珍惜穿校服的日子

我用的WebView和本地都开发混合应用程序。我使用ajax的POST方法在我的WebView,也对我的默认的Andr​​oid code。使用POST方法通过HttpClient的。但是,即使我去同一个服务器我的会话ID的不匹配对方。

I am developing hybrid application by using webview and native both. I am using ajax post method in my webview and also using post method via HttpClient on my default android code. But even if i go to same server my session id's doesn't match with each other.

有没有什么办法让同一个会话中的HTTP请求在我的应用程序?感谢您的任何建议。

Is there any way to make http request within same session in my application? Thanks for any advice.

推荐答案

我已经解决了这个问题:

I have solved this issue:

public void syncSession(final Context ctx){

    new Thread(new Runnable(){
        public void run(){

            //Products will be stated in memory 
            ProductManager pm = ProductManager.getInstance();

            //              HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpget = new HttpPost(UrlConstants.SERVICE_URL_SYNC);
            HttpResponse response;
            String result = null;
            try {
                response = httpclient.execute(httpget);
                //write db to 

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 



            List<Cookie> cookies = httpclient.getCookieStore().getCookies();

            if (! cookies.isEmpty()){

                CookieSyncManager.createInstance(ctx);
                CookieManager cookieManager = CookieManager.getInstance();

                //sync all the cookies in the httpclient with the webview by generating cookie string
                for (Cookie cookie : cookies){

                    Cookie sessionInfo = cookie;

                    String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
                    cookieManager.setCookie(UrlConstants.SERVICE_PRE_URL, cookieString);
                    CookieSyncManager.getInstance().sync();
                }
            }

        }
    }).start();
}