Wednesday, November 23, 2016

Storing Cookies on Android with OkHttp

Not those kinds of cookies... Unfortunately.
Coming from an iOS background, I like how Apple makes things that “just work”. One of the things they got right was the Foundation framework’s URLSession class. Sure there are iOS networking libraries that can do a lot more, but the built in networking handles a lot of the behind the scenes magic for you.

Which is why I was surprised to find that my favorite Android networking library, OkHttp, didn’t have any cookie handling by default. I actually just took this feature for granted. If Apple can do it, then surely the more technical folks can too?

Well after a grueling day of thinking, Googling, and frustration, I got it to work. And it’s not as hard as it seemed. The difficult part is just figuring out how to set it up.

The key is the CookieJar class. In order for it to work, you’ll need to attach it to your OkHttpClient instance before making any calls. First add it to build.gradle

compile 'com.github.franmontiel:PersistentCookieJar:v1.0.0'

Using it in the code would look something like this.


In my case, I had multiple AsyncTasks, so I chose to use the same client for both, hence the client and cookie jar are global. However, because the cookie jar uses shared preferences, this isn’t entirely necessary. The cookies are stored and retrieved from the same place, so in theory, you could create multiple instances of PersistentCookieJar, which is great if you need to access the same site from multiple activities or fragments.

You can now proceed to make the requests as normal, knowing your cookies will be persisted.

There’s just one other thing to ensure everything works properly; you still need to clear cookies manually. This is common when authenticating users. In my case, if the user types their password incorrectly before successfully logging in, the cookies are not properly saved and the server doesn’t realize they’re signed in. Some of the StackOverflow answers out there declare the type as CookieJar, but be sure to use PersistentCookieJar. That way you can clear cookies when you need to, for example, when the user attempts a new login.

mCookieJar.clear();

And there you have it. A little tricky to figure out, but quite simple and powerful. And all the hard work is done for you, just as we like it.

20 comments:

  1. Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks 9apps APK

    ReplyDelete
  2. Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks best iPhone backgrounds

    ReplyDelete
  3. I appreciate everything you have added to my knowledge base.Admiring the time and effort you put into your blog and detailed information you offer.Thanks. Tech Tricks

    ReplyDelete
  4. Along these lines, after the investigation, it appears that iOS has a slight edge over Android as a diversion improvement stage.address

    ReplyDelete
  5. Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post. Airshou Download

    ReplyDelete
  6. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. AntiRevoke Download

    ReplyDelete
  7. It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. Cydia iOS 11.3

    ReplyDelete
  8. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. 9apps

    ReplyDelete
  9. Thanks for the valuable information and insights you have so provided here... vidmate app download

    ReplyDelete
  10. compile 'com.github.franmontiel:PersistentCookieJar:v1.0.0'
    does not compile

    ReplyDelete
  11. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. top android emulators

    ReplyDelete
  12. This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine. Read more at MaddenMobilechampions.com

    ReplyDelete
  13. An impressive share, I recently given this onto a colleague who had been conducting a little analysis during this. Anf the husband in fact bought me breakfast since I ran across it for him.. smile. So permit me to reword that: Thnx for any treat! But yeah Thnkx for spending time to go over this, I believe strongly about it and adore reading more on this topic. If at all possible, as you become expertise, might you mind updating your blog post with increased details? It really is highly helpful for me. Large thumb up due to this article! brawl stars gems free

    ReplyDelete