Passing a JavaScript object using addJavascriptInterface() on Android
Asked Answered
C

6

50

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.

Cody answered 12/2, 2010 at 9:56 Comment(1)
Note, make sure you your parameter is not undefined. I tested and it does not get converted to null on Java side. It becomes the string 'undefined'.Schalles
T
51

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

Traynor answered 12/2, 2010 at 12:16 Comment(11)
@Robert Siemer: Use JSON, then.Traynor
Or... I call some Java method once for each item in the array. I expect to have less then ten items...Query
@Robert Siemer: If you're not going to call it a lot, implement whichever you find easier. If it's something you do frequently, benchmark them. I don't know how much overhead there is in the JS->Java bridge.Traynor
Does anybody know if there is actually any documentation on exactly what can be taken as parameters and what can be returned from a method which is part of the injected java object?Legg
@WayneUroda: Alas, I am not aware of any formal documentation.Traynor
From my tests, you can pass any primitive, string, of array of those. in case of polymorphism, though, it appears that the method that accepts float has priority (probably a matter of ordering the methods). I'll post something about that when I have investigated furtherPaschasia
@WayneUroda I'm also looking for some documentation. Does anybody know something in the mean time?Maynardmayne
serialize javascript object to json string and then pass that string to the android interface. now deserialize this json string in java to a java object.Multiplier
hey @Traynor can you please share any source code/ documentation link to back this up? In my case i need to capture an integer/float(which are both same in js) and i tried with java's Number class, it did not workBenia
@anshsachdeva: As I wrote 9 years ago, I am not aware of any formal documentation.Traynor
Here is some documentation: chromium.googlesource.com/chromium/src/+/master/android_webview/…Miserere
C
28

This is how I am doing...

In Android...

@JavascriptInterface
public void getJSONTData(String jsonData) {
      try {
             JSONObject data = new JSONObject(jsonData); //Convert from string to object, can also use JSONArray
          } catch (Exception ex) {}
}

In JavaScript...

var obj = { Name : 'Tejasvi', Age: 100};
var str = JSON.stringify(obj);
Android.getJSONTData(str);

As of now, I could not find any other proper way to pass the native JavaScript object directly to JavascriptInterface.

Calling Android.getJSONTData({ Name : 'Tejasvi', Age: 100}) results in null (if parameter type is Object) or undefined (if parameter type is defined as String) in getJSONTData.

Collision answered 21/9, 2015 at 14:55 Comment(0)
S
10

I found a solution, using JSON. My Java method returns a JSONArray, on my javascript code I receive this and convert to a javascript vector using JSON.parse(). See the example:

Java:

public class JavaScriptInterface {
Context mContext;
private static int ind=-1;
private static int [] val = { 25, 25, 50, 30, 40, 30, 30, 5, 9 };

public JavaScriptInterface(Context c) {
    mContext = c;
}

@JavascriptInterface
public JSONArray getChartData() {
    String texto = " [ {name: 'valor1', 2007: "+val[(++ind)%9]+"}, "+
                     " {name: 'valor2', 2007: "+val[(++ind)%9]+"}, "+
                     " {name: 'valor3', 2007: "+val[(++ind)%9]+"} ]"; 

    JSONArray jsonar=null;
    try {
        jsonar = new JSONArray(texto);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return jsonar;
}
}

Now the javascript code:

window.generateData = function() {
        /*var data = [ {name: 'valor1', 2007: 50},
                     {name: 'valor2', 2007: 20},
                     {name: 'valor3', 2007: 30} ];     */
        var data = JSON.parse( Android.getChartData() );
        return data;
    };

The commented code above show how it was when static, and now the data came from the Java code.

It was testes on Android 2.1 and 3.2.

Shiv answered 5/6, 2012 at 4:47 Comment(0)
M
9

I can run this feature

In Javascript :

var data = {
            'username' : $('#username').val().trim(),
            'password' : $('#password').val().trim(),
            'dns' : $('#dns').val().trim()
        }
        var str = JSON.stringify(data);
        Native.getLoginService(str);

In Android :

@JavascriptInterface
public void getLoginService(String jsonData){
    try{
        JSONObject data = new JSONObject(jsonData);
        String username = data.getString("username");
        String password = data.getString("password");
        String dns = data.getString("dns");

        Log.i("TAG",username + " - " + password + " - " + dns);

    }catch (Exception ex){
        Log.i("TAG","error : " + ex);
    }
}

Good luck with...

Mailemailed answered 13/11, 2017 at 21:42 Comment(0)
Q
3

You can't pass JSONObject or JSONArray, but you can send strings with that form and parse them to those types.

Your option is to expose the method using strings and then you can use the JSONObject or JSONArray to parse the string and use it accordingly.

Here is what I did.

@JavascriptInterface
public void passJSON(String array, String jsonObj) throws JSONException
{
    JSONArray myArray = new JSONArray(array);
    JSONObject myObj = new JSONObject(jsonObj);     
    ...

}

where array is '["string1","string2"]' and jsonObj is '{attr:1, attr2:"myName"}'

Quicksand answered 19/8, 2014 at 4:53 Comment(1)
This could be good answer if example and was clear and completeCollision
C
1

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.

Critical answered 21/7, 2010 at 3:38 Comment(2)
Where are the docs? There's only a tiny section devoted to it in WebView's documentation.Garboard
I can confirm this, but in JS space, it is not a real Javascript array. You'll get an object with methods like length() and get(), I could only use the specific get()s like getInt(). Basically I'm pretty sure you get everything here: json.org/javadoc/org/json/JSONArray.htmlDactylography

© 2022 - 2024 — McMap. All rights reserved.