Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android webview loadurl slow

I am working on an application where i am loading a webpage from a an external url in a webview. Loading the page take so much time to be loaded it take between 30 Seconds and 1 Minute so please take look here on my code

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WebActivity extends AppCompatActivity {

    private WebView webView;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        webView = (WebView)findViewById(R.id.webView);
        progressBar= (ProgressBar)findViewById(R.id.progressBar2);
        String link = getIntent().getExtras().getString("webLink");
        String title = getIntent().getExtras().getString("webTitle");
        setTitle(title);
        webView.setVisibility(View.GONE);
        progressBar.setVisibility(View.VISIBLE);
        Log.d("WEB", link);
        webView.setWebViewClient(new MyBrowser());
        webView.getSettings().setJavaScriptEnabled(true);
        //webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        //webView.getSettings().setDomStorageEnabled(true);
        //webView.getSettings().setAppCachePath(String.valueOf(getCacheDir()));
        //webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.loadUrl(link);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; goto parent activity.
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            webView.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);
        }
    }



}

any idea to improve the performance ?

like image 311
Mulham Aryan Avatar asked Feb 15 '16 14:02

Mulham Aryan


1 Answers

Unofortunately, there won't be much you can do to fix this. However:

  • Try to load your URL in the browser on your android device. Is it faster ? If not, there's not really much you can do.

There's a couple of things you can try though, and a few things to check. Specifically:

  • You're setting the visibility to View.GONE (making your webview invisible) while the page is loading, and then making it visible again when the page has loaded. This is probably the problem.

    Try without this, and you will probably find that it will be quicker. In my experience, onPageFinished(..) only fires some time after the page is loaded.

  • Does the page really require JavaScript ? If not, don't enable it.

  • If it's feaseable in your case, you can use a HTML parser like Jsoup to extract only the desired data from the page, and show that to the user. This will be a lot faster.

    If the page uses Ajax to load data dynamically, you can also load the data directly from the endpoints it uses. Open the page in a desktop browser, and open the network tab of developer tools to find out how the page works and loads data.

  • You can block requests from the WebView with shouldInterceptRequest(..). This may help if the page has things like eg. Facebook share buttons or extra images which you don't need. Blocking these will speed up load times.

If you show us the URL you're using, maybe I can investigate more and tell you axactly how you could speed it up in your case. Let me know if it helps.

like image 61
JonasCz Avatar answered Sep 29 '22 18:09

JonasCz