Seeking a solution for passing multiple values from an Android WebView to JavaScript. The challenge is that the string received in JS appears completely raw with control characters.
The specific issue arises when sending the following string from Java:
final String chartData = "{ \"data\": 1000, a: 10, b: 30, c: [ 5, 10, 15 ] }";
Instead of receiving the properly parsed string in JavaScript, the raw string is returned:
"{ \"data\": 1000, a: 10, b: 30, c: [ 5, 10, 15 ] }"
The desired output should be the java-parsed string:
{ "data": 1000, a: 10, b: 30, c: [ 5, 10, 15 ] }
Currently not handling any JSON, so the focus is on receiving the string correctly. Below is the code implemented so far:
WebViewActivity.java
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccessFromFileURLs(true);
final String chartData = "{ \"data\": 1000, a: 10, b: 30, c: [ 5, 10, 15 ] }";
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
webView.loadUrl("javascript:showAlert('message from android')");
webView.loadUrl("javascript:createChart('"+chartData+"')");
}
});
webView.loadUrl("file:///android_asset/report.html");
report.html
<div id="debugContainer">
debug
</div>
script.js
function createChart(lineChartData){
document.getElementById('debugContainer').innerHTML = JSON.stringify(lineChartData, null, 4);}