Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoopJ AndroidAsyncHttp and request cookies

Regarding to LoopJ AndroidAsyncHttp examples I make a get request like this:

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

I also add cookies:

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

But while making a GET request how can I send my cookies inside the request object ?

Regarding to documentation client has these method signatures:

 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) 

I would be happy if you can give an example that sends persistent cookies inside the GET request.

like image 505
trante Avatar asked Oct 22 '22 04:10

trante


1 Answers

Since you are creating your own PersistentCookieStore instance, Simply use the myCookieStore instance you created. Like @robotoaster says add it before get().

OR

Do this

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

Then follow instructions at http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/CookieStore.html .

Source: Cookies in loopj for android (Straight from loopj)

like image 78
Vrashabh Irde Avatar answered Oct 24 '22 05:10

Vrashabh Irde