2012-05-01 6 views
1

私はアンドロイドでサーバークライアントアプリケーションを開発していますが、アプリケーションのサーバー側でセッションを使用していますが、サーバー上でセッションを失うことがあります。 Ps:私はサーバー上でhttps接続を使用しています。Androidサーバー/クライアントアプリケーションセッションの問題

私はセッションを保持するためにこれらを使用しています:

  • 私は、単一のインスタンスDefaultHttpClientを使用し、すべてのHTTP要求のためにそれを使用しています。私はHTTPSのみ証明書のみを使用してhttpPost方法
  • を使用

  • schemeRegistry.register(新スキーム( "HTTPS"、のSSLSocketFactory、443)); ClientConnectionManager cm =新しいThreadSafeClientConnManager(params、schemeRegistry);

  • 私はすべてのHTTPリクエストの後に私のクッキーを保存します。

    プライベートボイドcreateSessionCookie(){

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

    }

私はこれらをやっているにもかかわらず、私はセッションを失います。 この問題を解決するのを手伝ってください。 アドバイスありがとうございました 最高のお付き合い

答えて

3

クッキーを手動で使用することは避けてください。CookieStoreをどこかに作成し、HttpContextに割り当てて、リクエストにそのコンテキストを使用してください。クッキーは自動的に保存され、復元されます。

private static CookieStore cookieStore = new BasicCookieStore(); 
private HttpClient httpclient = new DefaultHttpClient(); 
private HttpPost post = new HttpPost("your url here"); 

そして、この部分は、要求んメンバ関数、に入る::

これらはあなたのクラスのメンバーである私は、単一のインスタンスHttpPostメソッドを使用する

HttpContext ctx = new BasicHttpContext(); 
ctx.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 

HttpResponse result = httpclient.execute(post,ctx); 
+1

を持っていますか? – cagryInside

+0

いいえ、あなたは 'CookieStore'の一インスタンスしか必要としませんが、' HttpContext'を渡す限り、あなたは好きなだけ多くの異なるget/postリクエストを使うことができます。 。 – lenik

+0

あなたのコードを実装しましたが、うまくいきました。 – cagryInside