2012-03-20 16 views
1

webviewとnativeの両方を使用してハイブリッドアプリケーションを開発しています。私は自分のwebviewでajax postメソッドを使用しています。また、デフォルトのアンドロイドコードでHttpClient経由でpostメソッドを使用しています。 しかし、同じサーバーに行ってもセッションIDが一致しません。Androidのwebviewとjava httpクライアント間のHTTPセッション同期

アプリケーションで同じセッション内でhttpリクエストを行う方法はありますか? アドバイスありがとうございます。

答えて

4

私は、この問題は解決している:あなたはこの関数を挿入し、あなたがそれをどのように使用場所

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

はあなたが私に教えてくださいでした?ありがとう。 –

+0

どのようなhttpgetが使用されていますか? – hariszaman

関連する問題