I'm using the API 17 emulator to test a page containing a web view.
The webview first loads a page using the GET method.
Then the user submits the web form using HTTP POST method which causes a second page to load.
At this point if I rotate the screen I receive the "Webpage not available" error seen below. This only occurs if the page was loaded using the POST method. Note: I'm trying to restore the webview's state using webview.restoreState (see code below). Is there any way to tell Android to re-post the form data and reload the page instead of displaying this error message?!
I can't reproduce this same issue on KitKat, Lollipop, or Gingerbread... I can only reproduce this issue on Jellybean and Ice Cream Sandwich so far...
I've also confirmed this is an issue on an actual Nexus 7 device running Jellybean, so it's not an emulator-only problem.
Note: I'm not particularly interesting in using something like android:configChanges="orientation|keyboardHidden". As I understand it, this might solve my rotation issues, but the problem may still resurface if the activity state needs to be restored for other reasons.
Screenshots:
Step #1: Load the WebView Normally
Step #2: Submit the Form (uses HTTP Post Method)
Step #3: Rotate the screen to trigger webview.restoreState - error occurs
Code:
Here is some sample code to go along with my question. The code is in Mono C# but should be very nearly identical to Java.
public class MainActivity : Activity
{
WebView webview;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
webview = new WebView(this);
SetContentView(webview);
if (bundle == null)
webview.LoadUrl("http://2-dot-npwc-services.appspot.com/test-post.jsp");
else
webview.RestoreState(bundle);
}
protected override void OnSaveInstanceState(Bundle bundle)
{
base.OnSaveInstanceState(bundle);
webview.SaveState(bundle);
}
}
The sample HTML page that is performing the POST method looks like this:
<html>
<form action="test-post.jsp" method="post">
<input type="text" name="test" value="test"/>
<input type="submit"/>
</form>
<p>You entered: <%=request.getParameter("test")%></p>
</html>
The browser it's doing good (although it's a pain for us), you have 2 ways:
1- Keeping the webView instance and restoring the state, adding like you said android:configChanges="orientation|keyboardHidden"
2- Or reloading the post petition again.
I take the first one and if i get some error, i will go back to the last page. Doing static content, so on rotation no network or a new petition it's needed on each rotation, which if not, on server side will be a pain too.
To achieve the "if error go back" you need to set a Custom WebClient and override this method
webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
if ( webView.canGoBack() ) {
Toast.makeText(MainActivity.this, R.string.error_web, Toast.LENGTH_SHORT).show();
webView.goBack();
}
}
});
You can filter by errorCode to go back when you want, in some kind of error you can go back on other do other thing. I dont' know which error raises this POST request or if you want more filter on other situations, so i'm sure you can do a fine grain filter using it.
Edit: You have here the possible error codes
WebViewClient Error Codes
I hope this helps.
Related
Would like to ask for some advice on what's best way to implement on enabling and disabling the web view on Android?
I have this app wherein it can open urls within (by using web views) which then popups up and covers 80% of the UI, when the user navigates to another page of the app it should hide/close the web view but can be re-opened again when needed.
Here's a snippet of the code
private WebViewInterface webViewInterface = new WebViewInterface() {
#Override
public void onOpenURL(String url) {
navBrowserWV.setVisibility(View.VISIBLE);
navBrowserWV.getSettings().setJavaScriptEnabled(true);
navBrowserWV.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
handler.proceed();
}
});
navBrowserWV.loadUrl(url);
}
};
then this is how I close it
private void closeWebView() {
Log.d(LOGTAG, "closing webview...");
// Destroy WebView if it exists
if (this.navBrowserWV != null) {
this.navBrowserWV.stopLoading();
this.navBrowserWV.loadUrl("about:blank");
this.navBrowserWV.clearHistory();
this.navBrowserWV.clearCache(true);
this.navBrowserWV.pauseTimers();
this.navBrowserWV.setVisibility(View.GONE);
}
}
The problem with this implementation is that it displays the page properly at first but when I close it and then open the web view again with a url, it does not load the page anymore just a white background (no errors on the logger btw).
Would like to ask for help on how to resolve this one. Thanks
Finally, after a long time of digging up answers on the internet I found this old post regarding killing Android webview [link].
There's no real way to kill the WebView (it will always run in your process and you can't do anything about it ATM). So you only have to tell the WebView to load a bogus page, for me I did:
this.navBrowserWV.loadUrl("about:blank");
And it works now!
Quote from the website
4.1 .getSettings().setJavaScriptEnabled() Inhalt
Well, you would think that disabling JavaScript in the WebView’s settings would have an instant effect, in short: it does not. Only after reloading the page would there be no javascript any more, and this is not nice by design, since the page is still more or less well rendered.
4.2 .stopLoading() Inhalt
Since XHR „loads“ something from another server, you might think that calling „webview.stopLoading()“ would have an effect. In short: it does not. Works only on ressources contained within the HTML-file. Pity, is it not… Well, maybe not, since there is no „startLoading()“ method to resume XHR after resuming the activity anyway.
4.3 .destroy() Inhalt
As a last resort one might think about „destroy()“ing that thing, and true enough, the WebView itself is not accessible after that. Its threads however continue to exist as zombies somewhere in the vast RAM space and also continue to send XHR requests…
4.4 .pauseTimers() / resumeTimers() Inhalt
In short: Nope, does not work. I even don’t know what these methods are good for if not for controlling JavaScript timers. There aren’t any in plain HTML, AFAIK.
Update: When it comes to timers only, these functions seem to work on 2.3.5 and upwards, however, when there is no timer active at the time of calling the function, all in vain. With my use case: When pausing the app while there is an XHR active (instead of the running timer that schedules the next XHR call), nothing happens and the next timer continues unhindered.
I hope this will help someone who has the same problem as mine.
I'm trying to track what links my users click in the browser and failing miserably:
My code:
browser = new XWalkView(getMainActivity());
browser.setResourceClient(new XWalkResourceClient(browser)
{
#Override
public boolean shouldOverrideUrlLoading(XWalkView view, String url)
{
log.i("juhu 1", url);
return false;
}
});
This only calls the callback for URLs that I give it (browser.load()), but not for URLs that user then clicks on the rendered page. What's worse, it's not consistent: sometimes the callback gets called, sometimes not.
Here's an example that fails:
<html>
<body>
<p>my link 1</p>
<p>my link 2</p>
</body>
</html>
I tried this with XWalk 15.44.384.13 (latest) and 14.43.343.24 (a couple revisions back), both with no success.
I looked all over the place for similar methods, but neither resource client nor ui client seem to provide something that would work.
This looks fixed as of at least XWalk 16.45.421.19 with the call being made as expected.
I have a webview that shows ads (not my ads), the problem is when user clicks the "x" button to exit the ad, the ad still directs them to a site. What I wonder is since I can't control the ads, can I instead Disable page directing/forwarding inside webview? that means even if user clicks a link inside my webview nothing should happen.
You are looking for WebClient.shouldOverrideUrlLoading method.
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view, String url){
//True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.
return true;
}
});
I tried using shouldOverrideUrlLoading, but it didn't work. It looks like that this method is called only once when the html is loaded. After that, you click a link but the method is no more invoked.
I am also making a WebView embedding Youtube Player. Instead of forwarding ads redirect from the WebView, I prefer to open ads in a browser. So I override onLoadResource method:
#Override
public void onLoadResource(final WebView view, final String url) {
if(url.indexOf("googleadservices.")>-1){
view.getSettings().setJavaScriptEnabled(false);
view.stopLoading();
view.postDelayed(
new Runnable(){
#Override
public void run(){
Uri uri=Uri.parse(url);
Intent i=new Intent(Intent.ACTION_VIEW,uri);
i.setClassName("com.android.browser","com.android.browser.BrowserActivity");
startActivity(i);
}
}
,100
);
}
}
It worked. When I clicked the ads link, a new browser is opened in which ads site is displayed well, and the WebView was not redirected. When I push the return button, WebView show up again and I can continue watching video.
But there were still problems. If I repeat opening browser and returning to WebView for many times, the WebView might fail to block redirecting to the ads site. It is just redirected to the ads site. If I am lucky I could repeat opening and returning for 100 times. But sometimes It failed just when I repeat several times. I don't know why.
Does anyboday have any idea about how to improve it? Or is there another way to disable ads redirect?
You can build undetected webview build-id adblocker
I know it is too late for answering this question, however, for the sake of others who have the same question.
Well, you can build webview build-id adblocker, if you wish to prevent ads from loading, and provide smooth experience to the users, I am confident, because I have already implemented it in may app.
The Idea
Is to have a black list of all possible ad-serves domain name, then while webview load resources, you will prevent loading from black list domains. so it depends on how many ads-serves domain you have in the black list, fortunately, there is one website (pgl.yoyo.org/as/) which provide you with a very long list of ad-serves domai names, and listed them in many flavoures.
you can read this article for:
how to implement webview build-id adblocker
, you will build it %100 as long as you follow step by step instructions.
A summary of what we need to do:
Get the list of ad hostnames from pgl.yoyo.org.
Save the list somewhere, load it when application starts.
UseWebViewClient.shouldInterceptRequest(WebView, String) to intercept
requests.
Check if the request URL belongs to one of the hostnames in
the list and override it, returning a dummy resource instead of the
actual one, which is supposed to be ads
when I load some data in web view many times that white page appears ...just scroll it the data is return back..Am notice this issues on android 4.x ...I test it in android 2.3.3 webview work fine
what I can do to prevent this issues ?
I am facing a similar issue and finally solved but I am not sure why. Things that make it work was:
1)
When I finish with a WebView onDestroy of activity/fragment I have:
#Override
protected void onDestroy() {
super.onDestroy();
if (isFinishing()) {
if (myWebView != null) {
myWebView.destroy();
myWebView = null;
}
}
}
2)
I render the code via myWebView .loadDataWithBaseURL("file:///android_asset/", html_sourse, "text/html", "UTF-8", null); instead of myWebView.loadData(Uri.encode(html_sourse), "text/html", "utf-8");. When I am using the second line with an embedded css stylish inside html code page does not appear, changing to the first one (loadDataWithBaseURL) and adding to the html code a css from asset folder page appeared. I cannot understand why but works for me!
3) (optional)
Sometimes it is helpful to use clears if you do not care about keeping history:
myWebView.clearHistory();
myWebView.clearCache(true);
The webview with google search result links loaded in my android app, when I clicked on the links, it is opening up a blank screen.
I think, it is something to do with onmousedown event attached with every href links in the result page.
will be very thankful if I am provided with a way to handle this and make webview to actually openup the link that I am clicking on.
Here's another solution. After Google finishes loading the blank page you load the WebView with the previous page (which is the actual result) using the WebView's tag or a member variable. Like this:
#Override
public void onPageFinished(WebView view, String url)
{
System.out.println("onPageFinished: " + url);
if ("about:blank".equals(url) && view.getTag() != null)
{
view.loadUrl(view.getTag().toString());
}
else
{
view.setTag(url);
}
}
Here's my LogCat:
I/System.out(13182): onPageFinished: http://www.google.com/#hl=en&sugexp=les%3B&gs_rn=1&gs_ri=tablet-gws&cp=2&gs_id=9&xhr=t&q=amazon...
I/System.out(13182): onPageFinished: http://www.amazon.com/
I/System.out(13182): onPageFinished: about:blank
I/System.out(13182): onPageFinished: http://www.amazon.com/
I'm having the same problem. I get the "about:blank" page when I click on Google search results in a WebView in my Asus Transformer TF700 running 4.1.1. It doesn't happen in my Acer A100 tablet or other phones.
I noticed the result links work when I switch from Tablet to Classic version at the bottom of main Google Search Page.
https://www.google.com/?nota=1
If you remove the ?nota=1 in the above URL you won't see the Tablet option in your PC. In your tablet however the default google.com URL displays the Tablet option at the bottom.
If you use nota=1 in your search results page the links will work. Like this:
http://www.google.com/search?nota=1&q=amazon
I know this is not perfect. We'd like the default Google search results page's links to work. Google is doing something funky on the Tablet version which Asus Transformer doesn't seem to like!
I will post an update when I figure out what is causing this. Oh! the fun with Android :)
I wanted to add this as a comment to Stan Texan's answer, but I don't have enough rep points...
I'm testing on an older tablet (LG Optimus Tab/DoCoMo L-06c/T-Mobile G-Slate/Rogers LG V909) running Honeycomb (3.1) and my WebView is running into the same problem on Google's website.
Google's own browser works just fine when navigating to www.google.com, but when I use that url in a WebView in my app, the links are all "about:blank". 0_o
Therefore, if the user wants to go to Google's search page, I rewrite the url as follows:
//get the url from the user..
inUrl = urlAddressEditText.getText().toString();
// make sure it's properly formatted...
...
// then add the "/?nota=1&" if it's a Google domain.
String outUrl = inUrl.replaceFirst("(www\\.google\\..*/?)(\\??)", "$1/?nota=1&");
webView.loadView(outUrl);
So far this works, but I haven't tested it out for every scenario.
I had a look at your Vuwize app. Instead of embedding www.google.com directly into a WebView, you seem to have your own search form, run a Google search, and then render the result page yourself. This could explain your problem because result links in search results from Google is not straight href links to original pages. To understand more about your problem, it's necessary to see what exactly you're doing and how you run Google search from your server side.
If you just embed www.google.com in a WebView directly like this:
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
Users can enter search directly into Google and get result pages, where links are all clickable in the same WebView.
Here's another solution to handle blank pages caused in Jelly Bean. I found 2 URLs that cause blank pages. Override loadUrl(String) and not load them at all.
#Override
public void loadUrl(String url)
{
if (url != null && !(url.startsWith("file:///android_asset") || "about:blank".equals(url)))
{
super.loadUrl(url);
}
}