2012-01-09 8 views
3

私のAndroidプログラムとセッションCookieに問題があります。セッションCookieとAndroid

セッションは、cookieStoreを使用しているとはいえ、呼び出しの間に保持されていません。 WebサーバーコードはDJangoで書かれていますが、libcurlでCプログラムを使用するとセッションが保持されます。

public class JSON { 
... 
    DefaultHttpClient httpClient; 
    CookieStore cookieStore; 
    HttpContext httpContext; 
    HttpResponse response = null; 
    HttpPost httpPost = null; 
    HttpGet httpGet = null; 
public JSON(Context context) { 
    f_context = context; 
    updateSettings(); 
    HttpParams myParams = new BasicHttpParams(); 
    HttpConnectionParams.setConnectionTimeout(myParams, 10000); 
    HttpConnectionParams.setSoTimeout(myParams, 10000); 
    httpClient = new DefaultHttpClient(myParams); 
    cookieStore = new BasicCookieStore(); 
    httpContext = new BasicHttpContext(); 
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 
} 
... 
public String JSONSendCmd(String methodName) { 
    String getUrl = URL + "json/" + methodName+'/'; 
    httpGet = new HttpGet(getUrl); 
    try { 
      response = httpClient.execute(httpGet,httpContext); 
    } catch (Exception e) { 
    } 
    ... 
} 
+0

をおそらくので、あなたが新しいJSON()を伴わないとどのように複数のアクティビティ、全体でDefaultHttpClientを複数回使用して/開始します。 xxx.JSONSendCmd(); – yorkw

+0

実際、私が入力すると、私は認証し、私が離れるとき、私は認証を解除します。 – KevinA

答えて

4

これを試してみてください:

List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 

if (!cookies.isEmpty()) 
{ 

    CookieSyncManager.createInstance(ClientContext.getInstance()); 
    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(ClientContext.getResources().getString(R.string.domain), cookieString); 
     CookieSyncManager.getInstance().sync(); 
    } 
} 
関連する問題