2012-03-20 26 views
1

我正在通過使用webview和native兩者來開發混合應用程序。我在我的webview中使用ajax post方法,並在默認的android代碼中使用post方法通過HttpClient。 但即使我去了相同的服務器我的會話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

可以請你告訴我你在哪裏插入此功能,你如何使用它?謝謝。 – 2013-05-14 18:57:07

+0

httpget用於什麼? – hariszaman 2014-10-31 09:04:56