3

Similar Problem : JavaScript doesn't work on ICS

I'm working on an Android app that displays some code samples to the user. So, I'm using google-code-prettify in a WebView for syntax highlighting. But, the problem is, the js does not work on ICS (Ice Cream Sandwich) alone. It works perfectly on all other Android versions (2.2+) except 4.0.x. This is the code that I'm using.

WebView webView = (WebView) findViewById(R.id.webViewSample);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/code_snippets/sample_java.html");

The only error-like message that I get from logcat is UNKNOWN CHROMIUM ERROR: -6

Any help would be great. Thanks in advance.

Community
  • 1
  • 1
niranjan94
  • 754
  • 1
  • 8
  • 28
  • 2
    Is there any error message, exception trace, or other artifact that would let someone who doesn't have a device running 4.0.x and access to sample_java.html debug the problem? – Mike Samuel Sep 14 '13 at 15:07
  • 2
    @MikeSamuel The only _error-like_ message that i get from logcat is **UNKNOWN CHROMIUM ERROR: -6** – niranjan94 Sep 15 '13 at 06:55
  • 2
    I maintain prettify so I'd love to know the answer to your problem, but I have no way to try and replicate the problem, especially not without your code. [This search](https://www.google.com/search?q="UNKNOWN+CHROMIUM+ERROR%3A+-6") might point you at some similar code. – Mike Samuel Sep 15 '13 at 22:05
  • 2
    @MikeSamuel UPDATE: It works properly when I call the prettify js from the internet. The problem arises only when I call a local file. – niranjan94 Sep 16 '13 at 08:52
  • from what i have seen, UNKNOWN CHROMIUM *ERROR: -6* means the requested resource is not found. you can try to narrow it down by logging the url `shouldInterceptRequest` and looking which is the last called before the error. – njzk2 Dec 06 '13 at 19:39

1 Answers1

3

Didn't find any proper solution.. So, I ended up following MoshErsan's suggestion as mentioned here:

This is what I did:

WebView webView = (WebView) findViewById(R.id.webView);       
webView.setWebViewClient(new WebViewClient(){
    @TargetApi(11)
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        Log.d("shouldInterceptRequest", url);
        InputStream stream = inputStreamForAndroidResource(url);
        if (stream != null) {
            return new WebResourceResponse("text/javascript", "utf-8", stream);
        }
        return super.shouldInterceptRequest(view, url);
    }
    private InputStream inputStreamForAndroidResource(String url) {
        final String ANDROID_ASSET = "file:///android_asset/";
        if (url.contains(ANDROID_ASSET)) {
            url = url.replaceFirst(ANDROID_ASSET, "");
            try {
                AssetManager assets = getAssets();
                Uri uri = Uri.parse(url);
                return assets.open(uri.getPath(), AssetManager.ACCESS_STREAMING);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }           
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/code_snippets/sample_java.html");

Don't know what the actual problem is. But, this hack works.

Community
  • 1
  • 1
niranjan94
  • 754
  • 1
  • 8
  • 28