Android WebView上的Javascript接口的有效返回值是什么? [英] What are valid return values for a Javascript Interface on an Android WebView?

查看:69
本文介绍了Android WebView上的Javascript接口的有效返回值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有JavaScript的Android WebView,该JavaScript通过addJavascriptInterface方法调用Android方法:

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

myWebview.addJavascriptInterface(new JavascriptBridge(), "Android");

public class JavascriptBridge {

    public String getAString() {          
        return "my_str";
    }
}

这很好.我想将整数列表返回给WebView.试过这个:

This works fine. I want to return a list of ints to the WebView. Tried this:

public class JavascriptBridge {

    public int[] getMyInts() {          
        return new int[]{1,2,3};
    }
}

但是在JS中调用此函数将返回未定义的

but calling this function in JS returns undefined:

var myInts = Android.getMyInts();

是否存在Android Javascript接口的有效返回类型列表?仅仅是原语吗?

Is there a list of valid return types for an Android Javascript Interface? Is it only primitives?

推荐答案

我没有看到有效类型的列表(用于将值传递给Java函数并返回),但似乎只有基元和字符串有效.

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.

您可以使用JSON(例如,在Javascript中进行字符串化和解析,请在 json.org

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

这篇关于Android WebView上的Javascript接口的有效返回值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆