在Android的web视图和Java HTTP客户端之间的HTTP会话同步 [英] Http session synchronization between webview and java http client in Android

查看:200
本文介绍了在Android的web视图和Java HTTP客户端之间的HTTP会话同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的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();
}

这篇关于在Android的web视图和Java HTTP客户端之间的HTTP会话同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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