I need to save user login credentials for WebView in Android.
I have tried this, but with no luck.
WebView mWebview = (WebView) v.findViewById(R.id.wv_spielplan);
if(BuildConfig.VERSION_CODE < 21){
CookieManager.getInstance().setAcceptCookie(true);
} else {
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebview, true);
}
CookieSyncManager.createInstance(getActivity());
CookieSyncManager.getInstance().startSync();
If the webview displays internet content, you may want to deal with the data from server side. Online or if you are showing "off line" assets content, i.e., content packaged with your app, allow javascript and DOM to work and you will be able to set a cookie inside webview. Edit: have a look at this: https://stackoverflow.com/a/5409155/5885018 and Set a cookie to a webView in Android
Related
I am trying to write an app I want it to open a web page and auto login I am not sure how to go about sending the info to the browser from the app code.
So basically you are going to need to load in the webpage within a WebView (You can find instructions for that here and then probably push javascript into the WebView that will fill in the fields and load the page.
In your activity's onCreate:
WebView webview = new WebView(this);
setContentView(webview);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean onPageFinished(WebView view, String url) {
// Check here if url is equal to your site URL.
}
});
webview.loadUrl("http://yourwebsite.com/");
This line enables javascript in your WebView:
webView.getSettings().setJavaScriptEnabled(true);
Then you can use the WebViewClient to detect when the page you want has fully loaded. When that happens, you can use:
webView.loadUrl("javascript:document.getElementsByName('username').value = 'username'");
webView.loadUrl("javascript:document.getElementsByName('password').value = 'password'");
webView.loadUrl("javascript:document.forms['login'].submit()");
And it should automatically log you in. It's worth noting that this generally isn't easy to do on a lot of sites since they will randomize the login control ids and it also doesn't generally sit well with users if an application is logging into a website automatically for them.
How do I enable cookies in a webview?
I tried to use
CookieManager.getInstance().setAcceptCookie(true);
just before calling WebView.loadUrl() and it doesn't work as I get an HTML page error from a website saying cookies need to be enabled.
How does cookieManager know which webview to enable cookies?
Say if I had an activity with two webviews in the screen and I only wanted one of those webviews to enable cookies, how is that possible using a CookieManager?
I feel like I am missing something. I could not find a method like webView.setCookieManager or Cookiemanager.setWebView(webview).
You should consider that
CookieManager.getInstance().setAcceptCookie(true);
doesn't work from lollipop(API21) and above. You should check and use appropriate function for that case:
if (android.os.Build.VERSION.SDK_INT >= 21) {
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
} else {
CookieManager.getInstance().setAcceptCookie(true);
}
CookieManager.getInstance() is the CookieManager instance for your entire application.
Hence, you enable or disable cookies for all the webviews in your application.
Normally it should work if your webview is already initialized:
http://developer.android.com/reference/android/webkit/CookieManager.html#getInstance()
Maybe you call CookieManager.getInstance().setAcceptCookie(true); before you initialize your webview and this is the problem?
I want to load a web page in my webView.
Tried placing the webView.loadurl("") in AsyncTask's doinbackground / onpostexecute
and in the onresume.
The url is correct but nothing happens it just shows a white page. In the android manifest file internet access is enabled.
What else needs to be done to load a webview?
The application does not crash or show any error.
In my emulator I set the proxy with my user name and password.
Here is the code I use to load the URL:
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClientSubClass());
webView.loadUrl(promoURL);
I would recommend you to check what callbacks your webViewClient is getting. I'm guessing that the site requests an authentication, so override onReceivedHttpAuthRequest and do something like this
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
Log.d(TAG, "onReceivedHttpAuthRequest"));
handler.proceed(username, password);
}
Or if the authentication isn't the problem you can always overide onReceivedSslError to see if there is some certificate problem.
As the initial step though, I would recommend you to use the browser to see if you can load the page. I'm having some trouble with an https site that requires authentication, I enter my credentials and the site can't load(this is on android 2.3)
myVideoView = (WebView) findViewById(R.id.webView1);
myVideoView.setWebViewClient(new WebViewClientSubClass());
myVideoView.getSettings().setJavaScriptEnabled(true);
myVideoView.setPersistentDrawingCache(0);
myVideoView.getSettings().setPluginsEnabled(true);
myVideoView.requestFocus(View.FOCUS_DOWN);
myVideoView.loadUrl(promoUrl);
try this should work and also check whether it opens with https in normal browser.
I have an application on appspot that works fine through regular browser, however when used through Android WebView, it cannot set and read cookies. I am not trying to get cookies "outside" this web application BTW, once the URL is visited by WebView, all processing, ids, etc. can stay there, all I need is session management inside that application. First screen also loads fine, so I know WebView + server interactivity is not broken.
I looked at WebSettings class, there was no call like setEnableCookies.
I load url like this:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl([MY URL]);
}
..
}
Any ideas?
If you are using Android Lollipop i.e. SDK 21, then:
CookieManager.getInstance().setAcceptCookie(true);
won't work. You need to use:
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
I ran into same issue and the above line worked as a charm.
From the Android documentation:
The CookieSyncManager is used to synchronize the browser cookie
store between RAM and permanent storage. To get the best performance,
browser cookies are saved in RAM. A separate thread saves the cookies
between, driven by a timer.
To use the CookieSyncManager, the host application has to call the
following when the application starts:
CookieSyncManager.createInstance(context)
To set up for sync, the host application has to call
CookieSyncManager.getInstance().startSync()
in Activity.onResume(), and call
CookieSyncManager.getInstance().stopSync()
in Activity.onPause().
To get instant sync instead of waiting for the timer to trigger, the
host can call
CookieSyncManager.getInstance().sync()
The sync interval is 5 minutes, so you will want to force syncs
manually anyway, for instance in onPageFinished(WebView, String). Note
that even sync() happens asynchronously, so don't do it just as your
activity is shutting down.
Finally something like this should work:
// use cookies to remember a logged in status
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);
webview.loadUrl([MY URL]);
I figured out what's going on.
When I load a page through a server side action (a url visit), and view the html returned from that action inside a Webview, that first action/page runs inside that Webview. However, when you click on any link that are action commands in your web app, these actions start a new browser. That is why cookie info gets lost because the first cookie information you set for Webview is gone, we have a seperate program here.
You have to intercept clicks on Webview so that browsing never leaves the app, everything stays inside the same Webview.
WebView webview = new WebView(this);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url); //this is controversial - see comments and other answers
return true;
}
});
setContentView(webview);
webview.loadUrl([MY URL]);
This fixes the problem.
My problem is cookies are not working "within" the same session. –
Burak: I had the same problem. Enabling cookies fixed the issue.
CookieManager.getInstance().setAcceptCookie(true);
CookieManager.getInstance().setAcceptCookie(true); Normally it should work if your webview is already initialized
or try this:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.setAcceptCookie(true);
I have a requirement where there is a URL = "http://www.example/Open.pdf"
Now from my android application I want to open this PDF file directly in the default PDF viewer.
The moment I click on this link on the webpage, user should be presented with a default PDF viewer opened with this document.
Note: This file should not be stored on the SD card.
How do I proceed for this implementation?
We can open PDF file in the webview without caching it. Write below code in "onCreate" method .
Working code :
String url = "http://www.example.com/abc.pdf";
final String googleDocsUrl = "http://docs.google.com/viewer?url=";
WebView mWebView=new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
mWebView.loadUrl((googleDocsUrl + url));
setContentView(mWebView);
What happens here is you open the PDF using Google Docs. Best Advantage of using above method is the lazy loading of PDF. Does not matter how heavy the PDF is. Google Docs takes care of it.
You can view the pdf in the WebView using googleDocs.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
There is no way you can open a default PDF view from your application.
If your file is on the server and you want to open it without downloading then this might also pose a greater security concern. If external applications like default adobe reader can access the content on your server, then this is breaking the security framework altogether.
So, best option would be to launch a new instance of browser or webview and show the PDF document in google docs to the user.
This way user can read the document and get back to the recent state of the application as well.
You can view the pdf in the WebView using googleDocs.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
do you have the others solution besides view pdf file using http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf