How to return an array of String from Android

#1

I’m discovering devices on a network. I accumulate responses into an array of strings and after a timeout I return it to the web side of things.

I managed to get it working on Swift declaring the array as follow:

var responses: [Any] = []

And I resolve it like this

call.success([“data”: self .responses])

But I could not replicate it on Android

private List<String> responses = new ArrayList<String>();

private void resolve() {
JSObject ret = new JSObject();

String[] stockArr = new String[responses.size()];
stockArr = responses.toArray(stockArr);

ret.put(“data”, stockArr);
call.success(ret);
}

The response array is returned as [Ljava.lang.String;@519d71d

Is it possible to return arrays from Android?

Any help would be much appreciated

0 Likes

#2

try with JSArray instead of ArrayList, it’s an Array like class but that can be serialized for sending it to the javascript side.

0 Likes

#3

You are right! It also makes the code a lot cleaner

private JSArray responses = new JSArray();

private void resolve() {
JSObject ret = new JSObject();
ret.put(“data”, this.responses);
call.success(ret);
}

Thanks for your suggestion!

0 Likes