0

I am trying to load offline version of python documentation into an webview from assetfolder. The offline docs work perfectly in my pc web browser in offline but not working properly in webview (something like jquery is missing).

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView) findViewById(R.id.wrapper);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/python/index.html");
    }

}

And this error message is shown when I tried to load the home page or navigate to any page.

09-24 01:03:02.789: E/Web Console(479): ReferenceError: Can't find variable: $ at file:///android_asset/python/index.html:164

And the above error is for a Jquery code snippet ( I think this for that the jquery library isn't loading)

<script type="text/javascript">$('#searchbox').show(0);</script>

But when I load those pages from my local server localhost or http server, this is working perfectly. What did I miss?

Edit Showing nothing in the webView. After using loadDataWithBaseURL:

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView) findViewById(R.id.wrapper);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        AssetManager assetManager = getAssets();
        String htmlPage = null;
        InputStream input;
        try {
            input = assetManager.open("python/index.html");

            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            // byte buffer into a string
            htmlPage = new String(buffer);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            webView.loadDataWithBaseURL("file:///android_asset/", htmlPage, "text/html", "utf-8", "");

    }
}
4

2 回答 2

1

您的 html 页面应该参考

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js">
</script>

既然你说文档应该离线加载,jquery js 没有被加载。您可以将 jquery 与您的应用程序捆绑在一起,并像这样在本地引用它

  <script src="file:///android_asset/js/jquery-1.8.2.min.js"></script>

还包括

<script src="file:///android_asset/js/jquery.mobile-1.3.2.min.js"></script>

您可能还必须使用如下所示的 loadDataWithBaseURL 而不是 loadUrl 来加载您的 html 页面。

 AssetManager assetManager = getAssets();
            String htmlPage=null;
            InputStream input;
            try {
                input = assetManager.open("python/index.html");

                int size = input.available();
                byte[] buffer = new byte[size];
                input.read(buffer);
                input.close();
                // byte buffer into a string
                htmlPage = new String(buffer);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
webView.loadDataWithBaseURL("file:///android_asset/", htmlPage, "text/html", "utf-8", "");

注意: jquery-1.8.2.min.js 和 jquery.mobile-1.3.2.min.js 文件应该存在于您的资产文件夹中。

希望这可以帮助。

于 2013-09-24T17:31:55.443 回答
0

问题是 - 我从 phonegap 应用程序的资产文件夹中提取了这些网络文件;那些有一些额外的文件广告不同的结构。这就是为什么它不能在 Android 幼稚环境中工作!

于 2014-08-16T03:37:49.347 回答