如何将JSON格式的数据从WebView传递到HTML页面

时间:2012-04-11 22:40:53

标签: java javascript android json webview

我正在尝试将JSON格式的数据从我的Android WebView传递到HTML页面。但是,每当我尝试解析原始JSON数据时应用程序崩溃,我希望格式为{"key":"data"}

我的应用程序的目标是解释此JSON数据,将其形成一个值数组,并将其发送到HTML页面。这是正确的方法吗?

这是我的WebView代码......

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\"}";
    }
}

这是HTML代码......

<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> 

这是日志猫:

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

1 个答案:

答案 0 :(得分:8)

我复制粘贴你的代码并且它有效(没有显示因为你没有显示数据)但是从Javascript进入Android的回调是正确执行的。您可以使用以下代码进行检查:

    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();
    }

和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>