androidhtml-parsingandroid-webviewandroid-loader

Android: Unable to load simple html data having javascript file in WebView?


WebView web_view = (WebView) findViewById(R.id.webView1);
        web_view.getSettings().setJavaScriptEnabled(true);
        web_view.getSettings().setPluginsEnabled(true);
        web_view.getSettings().setAllowFileAccess(true);

        String data;
        data = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                + "<html>"
                + "<head>"
                + "<title>My First chart using FusionCharts XT</title>"
                + "<script type=\"text/javascript\" src=\"FusionCharts.js\">"
                + "</script>"
                + "</head>"
                + "<body>"
                + "<div id=\"chartContainer\">FusionCharts XT will load here!</div>"
                + "<script type=\"text/javascript\">"
                + "FusionCharts.setCurrentRenderer(\"javascript\");"
                + "var myChart = new FusionCharts(\"FusionCharts/Line.swf\", \"myChartId\", \"400\", \"300\", \"0\", \"1\" );"
                + "var dataString =\"<chart> <set label='0.00' value='0'/><set label='5.00' value='2' /><set label='7.00' value='3' /><set label='9.00' value='4' /><set label='12.00' value='2' /></chart>\"; "
                + "myChart.setXMLData(dataString);"
                + "myChart.render(\"chartContainer\");" + "</script>"
                + "</body>" + "</html>";

        Log.i("info", "Html " + data);

        web_view.loadData(data, "text/html; charset=UTF-8",null);

In my project i am using fusion charts. I am making a html string data and load it in WebView as in above code sample. When i run this html file in browser it runs and make me Fusion charts, But when i am doing this with for android Web view it is not loading in my Web View. I have already Enable javascript. I paste javascript file in to assets folder.


Solution

  • First of all, the browser you mentioned is a browser on your device or is it a browser in a computer?

    Now, if it worked in the android browser (or a browser in your device), then the problem is because the javascript files are on your assets folder. When you indicate a resource through relative path (the way you are using), the webview searchs it relative to the same folder as your html file is. Since you are using a String as your "html file", I would recommend using the loadDataWithBaseURL(). I made an usage example below using the assets folder as the base URL, try it.

    web_view.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8",null);
    

    public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl)

    Added in API level 1 Loads the given data into this WebView, using baseUrl as the base URL for the content. The base URL is used both to resolve relative URLs and when applying JavaScript's same origin policy. The historyUrl is used for the history entry.

    Note that content specified in this way can access local device files (via 'file' scheme URLs) only if baseUrl specifies a scheme other than 'http', 'https', 'ftp', 'ftps', 'about' or 'javascript'.

    If the base URL uses the data scheme, this method is equivalent to calling loadData() and the historyUrl is ignored.

    Parameters baseUrl the URL to use as the page's base URL. If null defaults to 'about:blank'. data a String of data in the given encoding mimeType the MIMEType of the data, e.g. 'text/html'. If null, defaults to 'text/html'. encoding the encoding of the data historyUrl the URL to use as the history entry. If null defaults to 'about:blank'.

    You can try passing the assets folder as the baseUrl, so my guess would be that your code would be like this

    Hope this helps!