的WebView加载在Android的本地HTML页面慢加载、页面、WebView、HTML

2023-09-04 12:14:21 作者:名字好难取

你好我加载处理一些JavaScript快讯webchromeclient后,本地的HTML文件, 但在我打电话的WebView的使用loadURL方法,我的本地HTML页面加载速度很慢等待大约20秒加载。

Hi i am loading local html file after handling some javascript alerts on webchromeclient, But after i call webview's loadUrl method my local html page loads very slowly it waits about 20 seconds to load.

下面是我下面的code:

Here is my code below:

@Override
    public boolean onJsAlert(WebView view, String url, String message,
            JsResult result) {
        // TODO Auto-generated method stub

        result.confirm();

        if (message.contains(GeneralConstants.ALERT_LOGIN_TIMUSER)) {
            String s[] = message.split(";");

            //Set ldap user 


            view.loadUrl("file:///android_asset/mainMenu.html");

            return true;
        }

感谢您的任何建议。

Thanks for any advice.

推荐答案

试试这块$ C $下有更​​好的表现。

try this piece of code for a better performance

    AssetManager mgr = getContext().getAssets();
                 try {
                     InputStream in = mgr.open(FileName,AssetManager.ACCESS_BUFFER);

                     String sHTML = streamToString(in);
                     in.close();

                     //display this html in the browser
                     WebView w = (WebView) findViewById(R.id.webview);
                     w.getSettings().setDefaultZoom(ZoomDensity.FAR);
                     w.loadDataWithBaseURL("file:///android_asset/", sHTML, "text/html", "utf-8", null);                        

                 } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }



public static String StreamToString(InputStream in) throws IOException {
        if(in == null) {
            return "";
        }

        Writer writer = new StringWriter();
        char[] buffer = new char[1024];

        try {
            Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

        } finally {

        }

        return writer.toString();
    }