2013-07-24 49 views
5

關於以循環J AndroidAsyncHttp examples我做一個GET請求是這樣的:循環J AndroidAsyncHttp和請求中的Cookie

final TextView text = (TextView) findViewById(R.id.textView); 
    AsyncHttpClient client = new AsyncHttpClient(); 
    client.get("http://example.com/mypage/", new AsyncHttpResponseHandler() { 
     @Override 
     public void onSuccess(String response) { 
      text.append(response); 
     } 
    }); 

我還加餅乾:

PersistentCookieStore myCookieStore = new PersistentCookieStore(this); 
    client.setCookieStore(myCookieStore); 
    BasicClientCookie newCookie = new BasicClientCookie("id", 17882); 
    myCookieStore.addCookie(newCookie); 

但是當一個GET請求,我怎麼能發送我的cookie在請求對象內?

關於到documentation客戶有以下方法簽名:

void get(Context context, String url, AsyncHttpResponseHandler responseHandler) 
void get(Context context, String url, Header[] headers, RequestParams params, AsyncHttpResponseHandler responseHandler) 
void get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) 
void get(String url, AsyncHttpResponseHandler responseHandler) 
void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) 

我會很高興,如果你可以給發送GET請求中持久Cookie的例子。

+1

要添加餅乾已經在設置client.setCookieStore(myCookieStore)。只要確保在client.get()之前添加它們 – robotoaster

回答

3

由於您正在創建自己的PersistentCookieStore實例,因此只需使用您創建的myCookieStore實例。像@robotoaster說的,在get()之前添加它。

OR

做這個

HttpContext httpContext = httpClient.getHttpContext(); 
CookieStore cookieStore = (CookieStore) httpContext.getAttribute(ClientContext.COOKIE_STORE); 

然後按照說明操作http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/CookieStore.html

來源:Cookies in loopj for android (直接從循環J)