0

I'm trying to extract cookie data from WebView in android and getting null exception.

override fun onPageFinished(view: WebView?, url: String?) {
    val cookies = 
    CookieManager.getInstance().getCookie("http://www.instagram.com")
    Log.d("result", "cookies:" + cookies)
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • You could start by following the troubleshooting instructions in: [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). Then you'll know a bit more about the problem. – Markus Kauppinen Oct 30 '18 at 12:16

1 Answers1

2

I think it's because you're loading a different url in your WebView instead of the one whcih you're trying to get cookies. ("http://www.instagram.com")

This is a more reliable solution to get cookies.

@Override
public void onPageFinished(WebView view, String url){
    final String cookies = CookieManager.getInstance().getCookie(url);
    Log.d("cookies", "here:" + cookies);
}

Here's the Kotlin way:

override fun onPageFinished(view: WebView?, url: String?) {
    val cookies = CookieManager.getInstance().getCookie(url);
    Log.d("cookies", "here:" + cookies);
}
savepopulation
  • 11,736
  • 4
  • 55
  • 80