Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL loads in chrome app, but not WebView

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

  • http://onlinelibrary.wiley.com/doi/10.1002/iid3.28/epdf
  • http://www.ajpmonline.org/article/S0749-3797(14)00628-X/fulltext

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);
like image 417
Wise Shepherd Avatar asked Apr 03 '15 17:04

Wise Shepherd


People also ask

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

Does Chrome use WebView?

Chrome, Google's ubiquitous browser, powers Android System WebView. The Android System WebView app is typically pre-installed on most Android smartphones. That way, every developer that resorts to using it to render internet content inside their app can do so without warning about compatibility issues.

Is WebView the same as Chrome?

The WebView shipped with Android 4.4 (KitKat) is based on the same code as Chrome for Android version 30. This WebView does not have full feature parity with Chrome for Android and is given the version number 30.0. 0.0.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.


2 Answers

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.

like image 105
Ugur Avatar answered Sep 26 '22 08:09

Ugur


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.

like image 22
Ircover Avatar answered Sep 22 '22 08:09

Ircover