2011-06-18 7 views
1

私はアンドロイドアプリと私のDrupalウェブサイトとの間にログインユーザーセッションを維持しようとしています。私の研究では、クッキーをDrupalに送り返すことになりますが、私は私を助けるためのリソースを見つけるのに苦労しています。Android HTTP cookie

私はAndroidとDrupalの間にクッキーを使用した永続的な接続を実現するための良いチュートリアルをお勧めしますか?

+0

そして、あなたがこれまで持って操作を行う際にCookieの保存を使用しますか?どのようにサイトにアクセスしていますか? – mschonaker

答えて

0

Android APIで提供されているHttpClientを使用すると、手動で接続を閉じるまで、クッキーによるセッション管理を行う必要があります。

私が間違っている場合は、CookieStoreインターフェイスまたはBasicCookieStoreクラスを使用して独自のCookieストアを実装することで、簡単に回避できます。他のすべてが失敗した場合は、手動でCookieを保存し、HTTPリクエストを行うたびにヘッダーにCookieを設定することができます。

これはあなたの特定の問題のためにどのように変化するかもしれませんが、これはあなたが与えた問題の説明を考えればほとんどうまくいくはずです。

1

他の誰が同じ問題を持って念のため、私は同様の問題を抱えていたし、私は次のコードでそれを解決することができました:

1-

CookieManager cookieManager; 
CookieStore cookieStore; 

あなたのクラスでCookieManagerとは、cookiestoreを定義します2 - デフォルトのCookieハンドラを追加します。クラスのコンストラクタ内やOnCreateの方法で

cookieManager = new CookieManager(); 
CookieHandler.setDefault(cookieManager); 

3 - あなたはHTTPリクエスト

public byte[] openURI(String uri) { 

    try { 
     URI uriObj = new URI(uri); 
     DefaultHttpClient client = new DefaultHttpClient(); 

     // Use the cookieStor with the request 
     if (cookieStore == null) { 
      cookieStore = client.getCookieStore(); 
     } else { 
      client.setCookieStore(cookieStore); 
     } 

     HttpGet getRequest = new HttpGet(uriObj); 
     HttpResponse response = client.execute(getRequest); 

     // Read the response data 
        InputStream instream = response.getEntity().getContent(); 
     int contentLength = (int) response.getEntity().getContentLength(); 
     byte[] data = new byte[contentLength]; 
     instream.read(data); 
     response.getEntity().consumeContent(); 
     return data ; 

    } catch (URISyntaxException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null;   
} 
関連する問題