Search code examples
androidsessioncookiesretrofitrobospice

Using Cookies with Robospice Retrofit requests


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.


Solution

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