javascript代码实例教程-Android webview与js 交换JSON对象数据

发布时间:2019-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-Android webview与js 交换JSON对象数据脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

最近几个项目的测试结果,AndROId无法主动通过调用

webview.loadUrl("javascript:"+callbackFunction+"('"+data+"')"); 这种方式将jsonobject类型的data传给js,因为js那边得到就是一个string的对象。

 

 

与此同时,js主动调用android的对象方式,android也无法返回给js一个jsonobject,需要js做一下转换,例如:

 


Android 代码:


[java] 
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);//不加上,会显示白边  
String  url="file:///android_asset/t.htML"; //js代码卸载t.html里 
NavigationInstance navigation =new NavigationInstance(this); 
mWebView.addJavascriptInterface(navigation, "Navigation"); 

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);//不加上,会显示白边
String  url="file:///android_asset/t.html"; //js代码卸载t.html里
NavigationInstance navigation =new NavigationInstance(this);
mWebView.addJavascriptInterface(navigation, "Navigation");

 

 


NavigationInstance里的代码:


[java]
@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; 

@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;
}

 

 

 

 

t.html里的代码:


[javascript] 
function testAPI(el){ 
    console.log("---------testAPI---------"); 
    eval("VAR obj = "+Navigation.GetManeuverInfo());  
     
    alert('tyPEof:'+typeof(obj)); 
    alert('maomao:'+obj.maomao); 
    alert('obj:'+obj); 
  } 

        function testAPI(el){
            console.log("---------testAPI---------");
            eval("var obj = "+Navigation.GetManeuverInfo());
           
            alert('typeof:'+typeof(obj));
            alert('maomao:'+obj.maomao);
            alert('obj:'+obj);
          }如果直接写成  Navigation.GetManeuverInfo.maomao是会提示undefined,因为js那边只得到了一个string对象而已,它不知道maomao是个key。

通过eval将其转化成表达式就可以调用Obj.maomao得到value。

 


在此ps一下ios,貌似人家支持webview很好,js可以直接获取到json对象.

 

 

 

默认t.html加载会自动执行testAPI函数,结果如下:

 


 

@H_832_126@Android webview与js 交换JSON对象数据

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-Android webview与js 交换JSON对象数据全部内容,希望文章能够帮你解决javascript代码实例教程-Android webview与js 交换JSON对象数据所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。