android - Send Object from Javascript to Kotlin using Webview - Stack Overflow

I have loaded a webpage using WebView ponent and added a JavascriptInterface. Please check the code bel

I have loaded a webpage using WebView ponent and added a JavascriptInterface. Please check the code below,

val webview = WebView(this)
setContentView(webview)
webview.settings.javaScriptEnabled = true
webview.loadUrl(HOME_PAGE_URL)
webview.addJavascriptInterface(JavascriptInterface(),”javascript_bridge”)

And when I call the invoke from Javascript using window.javascript_bridge.showToast(“Information Saved”);

private inner class JavascriptInterface
{
    @android.webkit.JavascriptInterface
    fun showToast(text: String?)
    {
        Log.d("WEBVIEW", text);
    }
}

I am able to call the method from Javascript to Kotlin without any trouble.

But now I want to pass an Object from Javascript to Kotlin like below,

var info = {
    message: “Information Saved”,
    ID: 123456
}

And when I call the invoke from Javascript using window.javascript_bridge.showToast(info);

I tried to change to the data type to Any, but the value passed from Javascript is null

private inner class JavascriptInterface
{
    @android.webkit.JavascriptInterface
    fun showToast(text: Any?)
    {
       Log.d("WEBVIEW", text.toString());
    }
}

I have loaded a webpage using WebView ponent and added a JavascriptInterface. Please check the code below,

val webview = WebView(this)
setContentView(webview)
webview.settings.javaScriptEnabled = true
webview.loadUrl(HOME_PAGE_URL)
webview.addJavascriptInterface(JavascriptInterface(),”javascript_bridge”)

And when I call the invoke from Javascript using window.javascript_bridge.showToast(“Information Saved”);

private inner class JavascriptInterface
{
    @android.webkit.JavascriptInterface
    fun showToast(text: String?)
    {
        Log.d("WEBVIEW", text);
    }
}

I am able to call the method from Javascript to Kotlin without any trouble.

But now I want to pass an Object from Javascript to Kotlin like below,

var info = {
    message: “Information Saved”,
    ID: 123456
}

And when I call the invoke from Javascript using window.javascript_bridge.showToast(info);

I tried to change to the data type to Any, but the value passed from Javascript is null

private inner class JavascriptInterface
{
    @android.webkit.JavascriptInterface
    fun showToast(text: Any?)
    {
       Log.d("WEBVIEW", text.toString());
    }
}
Share Improve this question asked Sep 11, 2019 at 1:54 PerseusPerseus 1,5884 gold badges30 silver badges55 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

As far as i know, the javaScript interface methods only accepts primitive types of data as parameters (as discussed on this question). If you still want to achieve that, you may serialize the object (to JSON format, for instance) in the javaScript and then Deserialize it in Java. Hope it helps :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745542419a4632187.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信