WebView loads URL 10 times slower than phone's Chrome browser
<p>I've been trying to figure this one out for quite a while, and yet haven't found a solution.</p>

<p>Inside my app I'm extracting a website's HTML through an invisible WebView component. I do not need to view the website, just get the html it loads. The website uses JavaScript to load all of its content, and therefore I need a full web renderer in order to execute that JavaScript. In its current form, I'm overriding the WebViewClient's onPageFinished method to inject JavaScript that dumps the html into a JavaScriptInterface which then processes it.</p>

<p>My main issue here is that when I load this URL inside my app it would take about 8 seconds, whereas loading the exact same URL in the phone's chrome browser it takes less than a second. Any suggestions as to what might be the problem?</p>

<p>If it could be of any help, the JavaScript that gets executed stores a cookie inside the WebView, and then tries to retrieve it, and only if it finds said cookie (which has a short expiration time) it would load the site's HTML. In this case it actually loads data formatted with JSON (about 370k characters).</p>

<p>Relevant WebView code:</p>

<pre><code>private void initWebView(View view) {
    class JSInterface{
        @JavascriptInterface
        public void processHTML(String scheduleJSONResponse){
            if(scheduleJSONResponse.length() &gt; 10000){
                Log.d(LOG_TAG, "Finished loading JSON");
            }
        }
    }
    webView = (WebView) view.findViewById(R.id.fragment_movies_web_view);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JSInterface(), "Android");
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.d(LOG_TAG, "Page finished loading");
            webView.loadUrl("javascript:window.Android.processHTML(document.getElementsByTagName('html')[0].textContent);");
        }
    });
    webView.loadUrl(url);
}
</code></pre>

<p>As for why I check that the length is more than 10k characters: Sometimes onPageFinished would get called more than once, with the first call being before the JavaScript is executed, and so I get HTML containing the script to be executed which isn't what I need.</p>

<p>Thanks in advance for any help!</p>

<hr>

<p>A logcat to demonstrate the issue:</p>

<pre><code>08-18 22:32:23.623 26238-26238/com.michaelsvit.kolnoa I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1e4dc08f time:502735009
08-18 22:32:23.638 26238-26238/com.michaelsvit.kolnoa W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 26238
08-18 22:32:31.336 26238-26238/com.michaelsvit.kolnoa D/MovieGridFragment: Page finished loading
</code></pre>

<p>Current code after simplifying it:</p>

<pre><code>private void initWebView(View view) {
    class JSInterface{
        @JavascriptInterface
        public void processHTML(String scheduleJSONResponse){
            if(scheduleJSONResponse.length() &gt; 10000){
                Log.d(LOG_TAG, "Finished loading JSON");
            }
        }
    }
    webView = (WebView) view.findViewById(R.id.fragment_movies_web_view);
    webView.getSettings().setJavaScriptEnabled(true);
    //webView.addJavascriptInterface(new JSInterface(), "Android");
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.d(LOG_TAG, "Page finished loading");
            //webView.loadUrl("javascript:window.Android.processHTML(document.getElementsByTagName('html')[0].textContent);");
        }
    });
}
</code></pre>