Android webview與js 交換JSON對象資料

來源:互聯網
上載者:User

最近幾個項目的測試結果,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函數,結果如下:

 


 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.