【问题标题】:Http session synchronization between webview and java http client in AndroidAndroid中webview和java http客户端之间的Http会话同步
【发布时间】:2012-03-20 22:24:28
【问题描述】:

我正在使用 webview 和 native 开发混合应用程序。我在我的 webview 中使用 ajax post 方法,并在我的默认 android 代码上通过 HttpClient 使用 post 方法。 但即使我去同一台服务器,我的会话 ID 也不匹配。

有没有办法在我的应用程序的同一会话中发出 http 请求? 感谢您的任何建议。

【问题讨论】:

    标签: android http httpclient android-webview httprequest


    【解决方案1】:

    我已经解决了这个问题:

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

    【讨论】:

    • 你能告诉我你在哪里插入这个功能以及你是如何使用它的吗?谢谢。
    • httpget 是做什么用的?
    猜你喜欢
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2014-12-13
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多