Using Cookies with Robospice Retrofit requests
Asked Answered
V

1

7

Can anyone please suggest me a way to manage cookies in robospice retrofit type HTTP requests.

I have a authentication system which has a login , a few GET HTTP requests , and a logout.

During login i need to save the session and use the same session for the rest GET HTTP requests and when I logout the session has to be cleared.

Here the login is a HTTP POST request which sends and recieves data through JSON format. I am using robospice retrofit as it easily manages the login and logout requests.

Ventura answered 27/6, 2014 at 18:2 Comment(0)
S
7

You can set system-wide cookie-handler via java.net.CookieManager

CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);

in your custom Application class.

To clear cookies after logout you can use method like this

public void clearCookies() {
    cookieManager.getCookieStore().removeAll();
}
Skywriting answered 29/6, 2014 at 7:1 Comment(2)
It works only for API >= 9 right ? I need the support for Android API 8 alsoVentura
@RevanthGopi yeah, that's right. You can check API level at runtime (as Retrofit does) and work with java.net.CookieManager's cookie management for API >= 9, and with Apache's BasicCookieStore for API < 9. See this question for one possible way of using BasicCookieStore: #18895040Skywriting

© 2022 - 2024 — McMap. All rights reserved.