views:

727

answers:

2

Is it possible to pass a JavaScript object from JavaScript to Java using addJavascriptInterface()? Something along these lines:

var javaScriptObject = {"field1":"string1", "field2":"string2"};
JavaScriptInterface.passObject(javaScriptObject);

How would such a call be captured on the Java side? I have no problem setting up the interface to send a string, but when I send an object, I receive null on the Java end.

+1  A: 

AFAIK, addJavascriptInterface() only works with primitive types and Strings, and so you cannot pass arbitrary Javascript objects.

CommonsWare
A: 

I think you can also pass JSONObject and JSONArray. So not only primitive types, but also primitive types stored in a javascript array [0,1,2] or dictionary {one:1, two:2}.

I have NOT verified this in code, just read the docs. Might be using it soon.

e mail