android - LoopJ AndroidAsyncHttp 和请求 cookie

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

我还添加了 cookie:

    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 的示例,我将非常高兴。

最佳答案

由于您要创建自己的 PersistentCookieStore 实例,只需使用您创建的 myCookieStore 实例即可。就像@robotoaster 说的那样,在 get() 之前添加它。

这样做

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)

关于android - LoopJ AndroidAsyncHttp 和请求 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17843788/

相关文章:

android - Android 中如何比较当前时间与第二天时间?

java - 如何在android中同步/限制某些异步http调用

java - 使用 SSL 协议(protocol)的 AsyncHttpClient

java - Android 间歇性 SSL Socket 读取超时

android - 如何在不压缩的情况下将位图转换为文件

android - AsyncHttpClient - 通过 HTTPS 的 POST 请求

android - AlarmManager PendingIntent.FLAG_NO_CREATE 取消警报后返回不为空

java - Android KeyStore 加密和解密数据

java - 如何将 onAcitivtyResult 用于 Android 的一些 fragment

java - 如何使我的 ListView 行看起来像 Twitter 应用程序 [屏幕截图]?