4

I am trying to load data into android webview using

webview.loadDataWithBaseURL("", htmlcontent, "text/html", null, "");

a method returns htmlContent from a StringBuilder which populates html data.

I have enabled javascript and set webChromeClient as follows

webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.addJavascriptInterface(new JSClass(), "Android");

my interface to javascript:

class JSClass {
    public void getHTMLContent(String html)
    {
        Log.i(Global.TAG, "HTMLContentReceived: "+html);
    }
}

and my javascript in html page:

<script type="text/javascript">
    var ele = document.getElementsByClassName('test');
    for(var i=0;i<ele.length;i++){
        ele[i].onclick = function(){
            window.Android.getHTMLContent(this.innerHTML);
        }
    }
</script>

but somehow the javascript is not returning any value. It works fine with loadData(url) where url is a simple webpage in assets folder

Please help Thanks in advance

1
  • Corrected!! just add javascript in your activity using webview.loadUrl("javascript://your javascript goes here"); after webview.getSettings().setJavaScriptEnabled(true); in above code and it works smoothly!! hope it helps Commented Jun 20, 2013 at 6:28

1 Answer 1

1

You don't have any baseURL to use, since you're loading a dynamical generated HTML. For this reason webview.loadData(htmlcontent, "text/html", null); should be more than enough.

Javascripts don't throw any exceptions in Java code. Remember that JS is not that type-safe/strict as Java code ... My way of doing is to put logs between sensitive Javascript calls to see if that line passes and to check values. Since you didn't provide the HTML, I would setup the WebChomeClient and override the onConsoleMessage:

webview.setWebChromeClient(new MyChromeClient());

private class MyChromeClient extends WebChromeClient {
        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            String message = consoleMessage.message() + " -- line " + consoleMessage.lineNumber();
            switch (consoleMessage.messageLevel()) {
            case ERROR:
                logErrorMessage(message);
                break;
            default:
                logInfoMessage(message);
                break;
            }
            return true;
        }

        private void logInfoMessage(String message) {
            Log.i("JSTag", message);
        }

        private void logErrorMessage(String message) {
            Log.e("JSTag", message);
        }


    }

From your JavaScript you would then call for example: console.log('check my value:' + (ele != null)). More info on this here.

Looking at your JavaScript code, I can't understand to what points this.innerHTML.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you gunar for your feedback. I have solved the problem and now my webview responds to user clicks. The problem i am facing is that i load my webview with a page which contains a table n when user clicks on any row, i call an intent which loads some images depending upon some cell data in that row. It works smooth on first click but the application closes without giving any error on the second click. I do not understand y.Is it because of the memory that images consume(each image is about 2MB bt i nly dwnld only thumbs which is approx 200kb when user clicks on a row)? how do i solve dis ?
As a first comment: each StackOverflow (SO) question should be specific enough. Once you asked something, if the answer you received for THAT QUESTION is OK, you move on. For your current question, I would open another thread. But, since you started it: a 200kb image will occupy about three more time in memory considering it will be decompressed. I believe you should re-consider your strategy at what you app is doing. Maybe you could get more help if you could state your actual problem in a new SO question.
No problem, does the current question still stand?
yes it does.. I download a number of image thumbs(resolution ht*wd: 225*300) from server when user clicks on a row in webview he views the thumbs and goes back to webview, the app closes without any error when he clicks on any row for second time
There should be something revealing in the Logcat when the app is closing. Can you post that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.