WebView not loading URL - android

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.

Related

Android with webview with remote website

My android app has a webview which directly goes to the remote PHP site and shows user the registration form.
My question is, once user get registered successfully I want to close the webview and start another activity which is in native view. But how can I know whether he/she got registered successfully or not?
Thanks
You can do it this way:
In your PHP code, when the user gets registered successfully, redirect user to a unique link(this link will be used in your android code). Let's assume the link you redirect user to is "https://blablabla.com/successfully_registered"
In the WebViewClient's OnPageStarted method used by your WebView, Get the URL and check if it's similar to your redirect URL we assumed you used in your PHP code.
For Example:
WebView wv = findViewById(R.id.webview);
wv.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
//check if URL is similar to redirect URL
if(url.contains("https://blablabla.com/successfully_registered")){
view.stopLoading();
//user have been successfully registered. Start another activity or do something else...
}
}
});

Auto login on web page

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.

Webview doesn't load the URL , but browser does

So from my first screen I am passing a URL to an activity to launch in webview. But when webview is launched , it shows "web page not available - The web page at URL might be temporarily down or it may have moved permanently to a new web address"
But when i launch the same URL in android browser, it works fine. Here is my code for launching that URL in webview
super.onCreate(savedInstanceState);
String url = "";
url = getIntent().getStringExtra("loginURL");
WebView urlWebView = new WebView(this);
urlWebView.setWebViewClient(new WebViewClient());
urlWebView.getSettings().setJavaScriptEnabled(true);
urlWebView.loadUrl(url);
this.setContentView(urlWebView);
What am I doing wrong?
I found the issue. The issue was that the URL I was using has https:// and SSL certificate for the URL was self-signed. The solution from Does the Web View on Android support SSL? helped me fixed the issue.
I added below part in my code
import android.net.http.*; //added this import statement
urlWebView.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
handler.proceed();
}
});
Hope this will help other users.
Make sure the url string you're loading has "www" prefixed to it, i.e. "https://www.google.com" and not "https://google.com"
Weirdly enough, this fixed the issue, at least on my end.

Webview client not listening to shouldOverrideUrlLoading method when clicking some other hyperlink/button in the webView

I have a sharepoint site, which has ntlm authentication. in order for me to load the page, i do an authentication to the site using this.
public String LoadUrlWithNTLM(String url){
CkHttp http = new CkHttp();
http.put_Login("username");
http.put_Password("password");
http.put_NtlmAuth(true);
http.put_SessionLogFilename("ntlmAuthLog.txt");
String source = http.quickGetStr(url);
return source;
}
and load the webview with this.
public void LoadWebView(String url, String source){
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadDataWithBaseURL(url, source, "text/html", "", "");
}
i call this in the OnCreate()
source= LoadUrlWithNTLM(url);
LoadWebView(url,source);
then i check if there is a url event click with this
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
String toWebView = LoadUrlWithNTLM(url);
LoadWebView(url,source);
return false;
}
});
at some point, i can manage through go to the Sharepoint Site with NTLM Authentication, but when i click some link, it just display "401 UNAUTHORIZED" and do not invoke the shouldOverrideUrlLoading() method on breakpoint.
After authorizing, each subsequent HTTP request should include an Authorization header that contains the result of the prior authorization. If the subsequent request were sent using Chilkat HTTP, then the object would automatically send this Authorization header. However, the WebView has no knowledge of it, and it's including any Authorization header with it's request, and therefore you get the "401 Unauthorized" error.
One solution is to see if you can do NTLM authorization with WebView. I'm assuming you're using Chilkat only because this is not possible.
Another solution is to use Chilkat as you are doing, but then get the value of the Authorization header (from Chilkat) and explicitly set this header field with WebView. I don't know enough about WebView to know whether this is possible. To get the value of the Authorization header from Chilkat may require a new Chilkat feature (and I think this may be easy to do). (or it's already possible, but in a convoluted way)

How to enable Cookies in WebView? [duplicate]

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);

Categories

Resources