LoopJ AndroidAsyncHttp并请求cookie

时间:2013-07-24 20:02:44

标签: android android-async-http

关于LoopJ AndroidAsyncHttp examples我发这样的获取请求:

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

我还添加了cookies:

    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 个答案:

答案 0 :(得分: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 (直接来自loopj)