Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass JSON-formatted data from a WebView to a HTML page

I am trying to pass JSON-formatted data from my Android WebView to a HTML page. However, the app crashes whenever I try to parse the original JSON data, which I am expecting to be of the format {"key":"data"}

The aim of my app will be to interpret this JSON data, form it into an array of values, and send it to a HTML page. Is this the correct approach to take?

Here is my WebView code...

public class MyWebView extends Activity {
    WebView mWebView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_view);

        mWebView = (WebView) findViewById(R.id.webviewSch);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(this, "webConnector");
        mWebView.loadUrl("file:///android_asset/table.html");
    }

    public String load() {
        return "{\"key\":\"data\"}";
    }
}

And here is the HTML code...

<html>
<head>
<title>Test</title>
<script type="text/javascript">

function loader() {
    var jsonData = window.webConnector.load();
}

</script>
</head>
<body onload="loader()">
Do nothing
</body>
</html> 

Here is the log cat:

04-15 00:35:44.551: W/dalvikvm(442): JNI WARNING: jarray 0x4053f1a0 points to non-array object (Ljava/lang/String;)
04-15 00:35:44.551: I/dalvikvm(442): "WebViewCoreThread" prio=5 tid=9 NATIVE
04-15 00:35:44.551: I/dalvikvm(442):   | group="main" sCount=0 dsCount=0 obj=0x4051bcc0 self=0x19b200
04-15 00:35:44.551: I/dalvikvm(442):   | sysTid=451 nice=0 sched=0/0 cgrp=default handle=1684280
04-15 00:35:44.551: I/dalvikvm(442):   | schedstat=( 206004592 365607782 69 )
04-15 00:35:44.551: I/dalvikvm(442):   at android.webkit.LoadListener.nativeFinished(Native Method)
04-15 00:35:44.551: I/dalvikvm(442):   at android.webkit.LoadListener.nativeFinished(Native Method)
04-15 00:35:44.551: I/dalvikvm(442):   at android.webkit.LoadListener.tearDown(LoadListener.java:1200)
04-15 00:35:44.551: I/dalvikvm(442):   at android.webkit.LoadListener.handleEndData(LoadListener.java:721)
04-15 00:35:44.551: I/dalvikvm(442):   at android.webkit.LoadListener.handleMessage(LoadListener.java:219)
04-15 00:35:44.551: I/dalvikvm(442):   at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 00:35:44.551: I/dalvikvm(442):   at android.os.Looper.loop(Looper.java:130)
04-15 00:35:44.551: I/dalvikvm(442):   at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:629)
04-15 00:35:44.551: I/dalvikvm(442):   at java.lang.Thread.run(Thread.java:1019)
04-15 00:35:44.551: E/dalvikvm(442): VM aborting
like image 806
DBoy Avatar asked Apr 11 '12 22:04

DBoy


People also ask

How do I show formatted JSON in HTML?

Use the JSON. stringify function to Display formatted JSON in HTML. If you have unformatted JSON It will output it in a formatted way. Or Use <pre> tag for showing code itself in HTML page and with JSON.

How do I display JSON data from API in HTML?

The fetch() method takes the URL of the JSON file as an argument and returns a Promise object. After resolving the Promise object we will get the JSON data in the Response object. We have the JSON data in data stored in a variable. Now we can use it to display the data in the webpage.

Can you convert JSON to HTML?

You can convert your JSON documents from any platform (Windows, Linux, macOS). No registration needed. Just drag and drop your JSON file on upload form, choose the desired output format and click convert button. Once conversion completed you can download your HTML file.


1 Answers

I copy pasted your code and it works (nothing is shown because you dont show the data) but the callback made from the Javascript into Android is executed correctly. You can check it with this code:

    WebView mWebView = (WebView) findViewById(R.id.webView1);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.addJavascriptInterface(this, "webConnector");
    mWebView.addJavascriptInterface(this, "toaster");
    mWebView.loadUrl("file:///android_asset/table.html");
    }

    public String load() {
        Log.e("HelloJavascript","HelloJavascript");
        return "{\"key\":\"data\"}";
    }

    public void print(String message){
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

And the HTML

    <html>
    <head>
    <title>Test</title>
    <script type="text/javascript">

    function loader() {
        var jsonData = window.webConnector.load();
        toaster.print(jsonData);
    }

    </script>
    </head>
    <body onload="loader()">
    Do nothing
    </body>
    </html> 
like image 111
Addev Avatar answered Oct 13 '22 00:10

Addev