7

Why do some pages not load in my WebView (but seem to work fine in Chrome)? For example:

I've set my WebView to enable JS. Here is my WebView init code:

    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setDomStorageEnabled(true);
    webView.setWebViewClient(new WebViewClient());

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {

            progressBar.setProgress(progress);

            if (progress == 100) {
                progressBar.setVisibility(View.GONE);
            }
            else {
                progressBar.setVisibility(View.VISIBLE);
            }
        }
    });

    webView.loadUrl(url);
Wise Shepherd
  • 2,228
  • 4
  • 31
  • 43

4 Answers4

2

I have a very similar webview code on my project, I just replaced the urls you have provided.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_agreement);
    WebView webView = (WebView) findViewById(R.id.userAgreementView);
    final ProgressWheel progressWheel = (ProgressWheel) findViewById(R.id.progress_bar);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setDomStorageEnabled(true);
    //webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
    Log.v("UA", webView.getSettings().getUserAgentString());
    String url = getIntent().getStringExtra("data"); //Urls provided by other activity
    if (url != null && !url.equals("")) {
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {

                 //progress wheel fills here etc.
                 // ...
                 // ...  
                }
            }
        });
    webView.loadUrl(url);

    }

}

http://onlinelibrary.wiley.com/doi/10.1002/art.39115/pdf this one loaded like a charm in the webview with no glitches and no wrong behavior.

Then I tried this one http://www.ajpmonline.org/article/S0749-3797(14)00628-X/fulltext, when the webview completes loading a new native Chrome activity starts immediately and loads the mobile version of the page in a separate activity.

I checked the behavior of the website with my desktop Chrome, when I make an http request with a Android user agent string it turns out to do the following things.

  1. Return 302 status code and redirect to http://www.ajpmonline.org/action/mobileChoice?originalRequestUri=%2Farticle%2FS0749-3797%2814%2900628-X%2Ffulltext&userInterface=mobile
  2. Set a cookie called MobileUI and redirect to original page with an 302 status code.
  3. Load the mobile version of the page depending on the MobileUI cookie. At that point website may be running a javascript code to popup a new window, which explains the behavior on my Galaxy S4 mini.

Then I decided mimic the user agent string as a desktop browser. I picked a UA string from http://www.useragentstring.com/pages/Chrome/ and set it like this before the loadUrl method and voilà!

    webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
    Log.v("UA", webView.getSettings().getUserAgentString());

This may be an edge case scenario and depending on the URL you are loading behavior may change but you can force to load desktop version of the websites with a proper user agent string.

Ugur
  • 1,679
  • 14
  • 20
  • Unfortunately I can't set the user agent as a desktop browser, since I want the mobile pages to load. As for the first link, I realize now that I had a login cookie set before trying the URL. The WebView only has problems loading when it access the actual content at the URL source, which you don't see because you haven't logged in. Let me see if I can find a better example for that one. – Wise Shepherd Apr 06 '15 at 16:17
  • In this case you can change the content of the webpage which causes problems. http://stackoverflow.com/a/8274881/3399234 that answer shows how to do it, but for sure this is not a reasonable and efficient solution. – Ugur Apr 06 '15 at 20:02
  • you can try with this URL http://onlinelibrary.wiley.com/doi/10.1002/iid3.28/epdf – Wise Shepherd Apr 08 '15 at 17:57
2

I'm using this code:

    WebSettings settings = webView.getSettings();
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    settings.setJavaScriptEnabled(true);

    settings.setAppCacheEnabled(false);
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setDatabaseEnabled(false);
    settings.setDomStorageEnabled(false);
    settings.setGeolocationEnabled(false);
    settings.setSaveFormData(false);

Hope it can help you.

Ircover
  • 2,406
  • 2
  • 22
  • 42
0

I looked around, first for this url : http://www.ajpmonline.org/article/S0749-3797(14)00628-X/fulltext it seems that they are using 'jwplayer' and i think since WebView didn't handle it, it launchs the 'real' web browser.

This code worked for the url above:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().contains("secure.jbs") || Uri.parse(url).getHost().contains("ajpmonline"))
            return false;

        Log.d("MYWEBVIEWCLIENT", "This url couldn't not be handled by the webclient : " + Uri.parse(url).getHost());
        return true;
    }
}

I agree it's not the best way to do it. I don't know the purpose of your webView, you can implement your custom WebViewClient if you already know urls that will loaded inside.

issathink
  • 1,220
  • 1
  • 15
  • 25
0

For me it was a stupid thing, the webview's height was wrap_content It should be match_parent

Kammaar
  • 1,611
  • 1
  • 14
  • 12