What are valid return values for a Javascript Interface on an Android WebView?

前端 未结 2 1916
孤街浪徒
孤街浪徒 2020-12-16 10:39

I have an Android WebView that has JavaScript that is calling Android methods through the addJavascriptInterface method:

myWebview.addJavascriptInterface(new         


        
相关标签:
2条回答
  • 2020-12-16 10:49

    You can return anything, primitives or Objects. Objects are converted to strings via their toString() method. If the JavascriptInterface method returns a JSON string (e.g. an Object who's toString() outputs JSON) you can then parse that string directly:

    var array = JSON.parse(window.android.getSomeJsonThing());
    
    0 讨论(0)
  • 2020-12-16 11:03

    I have not seen a list of valid types (for passing values to Java functions and to return), but only primitives and string seem to work.

    You can use JSON (e.g. stringify and parse in Javascript, check various Java options at json.org

    0 讨论(0)
提交回复
热议问题