Example of JSON object data exchange between Android webview and js

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.