Android cannot actively call the test results of recent projects.
Webview. loadUrl ("javascript:" + callbackFunction + "('" + data + "')"); this method transmits data of the jsonobject type to js, because js gets a string object.
At the same time, js actively calls the android object method, and android cannot return a jsonobject to js. js needs to perform a conversion, for example:
Android code:Copy codeThe Code is 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 this parameter is not added, the white edge is displayed.
String url = "file: // android_asset/t.html"; // JS Code unload t.html
NavigationInstance navigation = new NavigationInstance (this );
MWebView. addJavascriptInterface (navigation, "Navigation ");
Code in NavigationInstance:Copy codeThe Code is 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 t.html:Copy codeThe Code is 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 it is written as Navigation. GetManeuverInfo. maomao, the system will prompt undefined, because js only has a string object, and it does not know that maomao is a key.
You can call obj. maomao to obtain the value by converting eval into an expression.
Ps ios here. It seems that webview is well supported. js can directly obtain json objects.
The testAPI function is automatically executed when javast.html is loaded. The result is as follows: