I have an Android application that uses WebView and HTTP cookies. This application works on Android devices running API 19 or below. API 21 is not saving the http cookie for later reference.
Android WebView Code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_token);
WebView mWebView = (WebView) findViewById(R.id.activity_main_webView1);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebChromeClient(new WebChromeClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setDatabaseEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
mWebView.loadUrl("file:///android_asset/index.html");
}
Android Manifest
uses-sdk android:minSdkVersion="16" android:targetSdkVersion="19"
uses-permission android:name="android.permission.INTERNET"
Server Side Code creating cookie:
Response.Cookies("mycookie")("myvalue") = "123456789"
Response.Cookies("mycookie").Expires = Date() + 10
Response.Cookies("mycookie").Secure = True
Server Side Code reading cookie:
Response.Write Request.Cookies("mycookie")("myvalue")
When this runs on API 19 or below I can read/write cookies no problem. I am using cookies as you would with visiting any web page that uses cookies. Any help would be appreciated.
By doing so, whenever a cookie is set by the API through the API call using the particular instance of okHttpClient , the cookie will be stored automatically and will be used by Webview launched by the App.
Open your browser. Android browser: Go to Menu > More > Settings or Menu > Settings > Privacy & Security. Chrome: Go to Menu > Settings > Privacy. Android browser: Tap Clear cache, Clear history, and Clear all cookie data as appropriate.
android.webkit.CookieManager. Manages the cookies used by an application's WebView instances. CookieManager represents cookies as strings in the same format as the HTTP Cookie and Set-Cookie header fields (defined in RFC6265bis).
API 21 or Lollipop requires this to be added to your APP
if (Build.VERSION.SDK_INT >= 21) {
// AppRTC requires third party cookies to work
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(mWebView, true);
}
Works again!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With