Test results for recent projects, Android cannot proactively invoke
Webview.loadurl ("javascript: +callbackfunction+" (' +data+ ")"); This way the jsonobject type of data to JS, because JS over there is a string object.
At the same time, JS active call Android Object way, Android can not return to JS a jsonobject, need to do a bit of JS conversion, for example:
Android Code:
Copy Code code as follows:
WebView Mwebview = (webview) This.findviewbyid (R.id.webview);
WebSettings settings = Mwebview.getsettings ();
Settings.setjavascriptenabled (TRUE);
Settings.setpluginsenabled (TRUE);
Settings.setallowfileaccess (TRUE);
Settings.setcachemode (Websettings.load_no_cache);
Mwebview.setscrollbarstyle (View.scrollbars_inside_overlay), if not plus, will show white edge
String url= "file:///android_asset/t.html"; JS Code Uninstall t.html
Navigationinstance Navigation =new navigationinstance (this);
Mwebview.addjavascriptinterface (Navigation, "navigation");
Code in the Navigationinstance:
Copy Code code as follows:
@Override
Public Jsonobject Getmaneuverinfo () {
try{
Jsonobject test=new jsonobject ();
Test.put ("Maomao", "value");
return test;
return new Jsonobject (bean. Maneuverinfo);
}catch (Exception e) {
LOG.E (TAG, "", e);
}
return null;
}
Code in the t.html:
Copy Code code as follows:
function Testapi (EL) {
Console.log ("---------testapi---------");
Eval ("var obj =" +navigation.getmaneuverinfo ());
Alert (' typeof: ' +typeof (obj));
Alert (' Maomao: ' +obj.maomao);
Alert (' obj: ' +obj);
}
If you write Navigation.GetManeuverInfo.maomao directly, you will be prompted to undefined, because JS has only got a string object, it does not know Maomao is a key.
By using Eval to convert it to an expression, you can call Obj.maomao to get value.
In this PS iOS, looks like others support WebView very good, JS can get directly to the JSON object.
The default t.html load automatically executes the TESTAPI function, and the result is as follows: