Android WebView and JS swap JSON object Data sample _android

Source: Internet
Author: User
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:
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.