我在Java中有一个对象,其方法用@JavaInterface
注释。当我尝试返回一个数组时,比如String [],结果以undefined
的形式暴露给JS。
我希望能够传递至少可迭代的对象,无论包含什么类型,但不知道如何实现[Symbol.Iterator],因为它是属性< / em>,他们无法通过。
更新
抱歉,结果不会以undefined
的形式向JS公开。实际上,该值取决于Java方法返回的类型。如果它是Object
,则结果是一个空的不可迭代对象。
爪哇:
@JavascriptInterface
public Object foo(String p1, String p2)
{
return new String[]{"a","b","c"};
}
JS:
console.log('Object.getOwnPropertyNames(catalogItems): ' +
Object.getOwnPropertyNames(catalogItems));
for (let item of catalogItems)
{
text += item.text;
}
日志:
D/WebView: catalogItems: [object Object] -- From line 18 of file:///android_asset/content.html
D/WebView: Object.getOwnPropertyNames(catalogItems): -- From line 23 of file:///android_asset/content.html
D/WebView: Uncaught TypeError: catalogItems[Symbol.iterator] is not a function -- From line 28 of file:///android_asset/content.html
如果方法返回String[]
:
@JavascriptInterface
public String[] foo(String p1, String p2)
{
return new String[]{"a","b","c"};
}
似乎该方法根本没有被调用。