3

I have created an Android application, in that I want to get Session from webview.

How to make it possible ?

Thanks.

Sweta Sharma
  • 115
  • 1
  • 11
  • post what you've tried till now; did you try CookieManager.getInstance()? – Ahmad Sanie Jul 27 '15 at 10:20
  • @AhmadAlsanie, tried this method but it gives cookie data, I stores my items in Session, so how to get session data in webview? – Sweta Sharma Jul 27 '15 at 12:33
  • Do you need it for log in purposes ? – Ahmad Sanie Jul 27 '15 at 12:40
  • No, actually my site is for online shopping with limited fixed price items and I stores cart in session, so for checkout I use Paypal for web and I want to implement inapp purchase for android app, so I need cart from session in Android. – Sweta Sharma Jul 27 '15 at 12:44
  • okay walk me through the process of creating your session (Do you use HTTPURLConnection) ? other details about the code would be beneficial – Ahmad Sanie Jul 27 '15 at 12:51

2 Answers2

0

I use this method for getting session cookies from a webview:

public static String getCookieFromAppCookieManager(String url) throws MalformedURLException {

   CookieManager cookieManager = CookieManager.getInstance();
   if (cookieManager == null)
        return null;
    String rawCookieHeader = null;
    URL parsedURL = new URL(url);
    rawCookieHeader = cookieManager.getCookie(parsedURL.getHost());
    if (rawCookieHeader == null)
        return null;
    return rawCookieHeader;
}
Decoy
  • 1,598
  • 12
  • 19
  • what do you know ? I use this in production code. And I tested a lot of other ways like XWalkCookieManager etc. Maybe this snippet came from stackoverflow, so what ? @Kay U a purist? – Decoy Jul 27 '15 at 11:10
  • @Kay Yes, you are completely right mr. perfectionist. – Decoy Jul 27 '15 at 11:15
  • @Decoy, Thanks for answer, this is working for getting cookies, but i stores some data in session, so how to get it. – Sweta Sharma Jul 27 '15 at 12:39
-1

There are two ways:

  • If a developer has made a httpclient, and makes an api auth call and store the cookie. Then you sync the httpclient's cookie with webview and maintain a session natively.

  • If user has used a webview to make an auth call and the cookie resides in the webview.

First one is your code and simply making a getter will return instance of DefaultHTTPClient. The instance will have access to cookies too. you can make async calls to auth api to get correct cookie in the instance. Make sure to keep HttpClient and Webview in sync.

For retrieving cookie in second method, you would use CookieManager object and the url which user is logged into and you need cookie for, example twitter.com See the second post here for implementation details.

Community
  • 1
  • 1
Karan
  • 2,120
  • 15
  • 27