5

I have an application that makes several web calls in order to get authenticated after which a JSON is returned. My web calls are to an https server and I am using HTTPURlConnection.

I need to store the session in a cookie, after researching around, I found this

http://developer.android.com/reference/java/net/HttpURLConnection.html

Under the sessions with cookies header, it tells you to use this code here

   CookieManager cookieManager = new CookieManager();
   CookieHandler.setDefault(cookieManager);

However when I try using this code, the new CookieManager(); part highlights in red and says

The constructor CookieManager is not visible

and the Cookiehandler.setDefault also highlights in red and says

The method setDefault(CookieHandler) in the type CookieHandler is not applicable for the arguments (CookieManager)

Does anyone know why this is?

Thanks in advance!

ThinkingMonkey
  • 12,011
  • 12
  • 51
  • 80
AdamM
  • 4,205
  • 5
  • 45
  • 89
  • 4
    Check your import to see if you are using the right `CookieManager` class - which should be `java.net.CookieManager` – Rajesh May 08 '12 at 14:37
  • Could you post your code please. According to http://developer.android.com/reference/java/net/CookieManager.html there is definitely a public constructor. Are you using the right CookieManager class? – wattostudios May 08 '12 at 14:38
  • Rajesh was right, I changed the import, didn't work, so then tried clicking fix project set up, and worked. Cheers Rajesh – AdamM May 08 '12 at 14:39

1 Answers1

10

You're probably trying to use the wrong CookieManager class. In Android there are 2 classes...

android.webkit.CookieManager
java.net.CookieManager

For this context, you need to use the java.net.CookieManager class.

wattostudios
  • 8,446
  • 13
  • 40
  • 54
  • Got it working now, however when I try to run my application it crashes and says no class found error on java.net.CookieManager. Any idea why that happens? – AdamM May 08 '12 at 14:45
  • It sounds like you're missing the connection to your Android library package. Check the android settings and make sure that you're including one of the Android libraries. – wattostudios May 08 '12 at 14:48
  • Ahh, after doing some research found http://stackoverflow.com/questions/6354294/urlconnection-with-cookies Says that only works on API 9 upwards, so no use to me sadly – AdamM May 08 '12 at 14:53
  • Bugger. As a suggestion, if you're feeling adventurous, you might be able to get the source code for the API9 version of `CookieManager` and add that in to your project as `com.myprogram.CookieManager` (not sure how much work this would involve though, if there's a lot of dependencies). – wattostudios May 08 '12 at 14:56
  • The dependencies would worry me with that method, will just have to look for an alternative, bleh!!! – AdamM May 08 '12 at 15:00