Learn how to send data from java to javascript with cordova

In cordova, you may need to return some data to the Javascript view (not only simple strings) from Java. This data can be efficiently and easily sent to the view with JSON, however we will not create a JSON string from an array manually because that's not a good practice, you can use the the org.json library, already available with Cordova.

Include the required components

We need to include the following components in the class, then we will be able to create json arrays and objects (if you use try and catch block , you need to include the JSONException).

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

Creating a JSON Object in Java

In javascript the creation of a object is really easy, you just need to assign the property name to a object and that's it (with point or brackets notation). With java is simple too, but different.

A JSONObject constructor can be used to convert an external form JSON text into an internal form whose values can be retrieved with the get and opt methods, or to convert values into a JSON text using the put and toString methods. A get method returns a value if one can be found, and throws an exception if one cannot be found. An opt method returns a default value instead of throwing an exception, and so is useful for obtaining optional values.

As in this case, we only want to send data from Java to Javascript, we will create a variable with type JSONObject and we will use the put function to add a key.

JSONObject item = new JSONObject();
item.put("name", "filename.java");
// in javascript this is something like
// {filepath:"aimaginarypath",name:"filename.java"}

the variable item contains name and filepath keys as a normal javascript object.

Creating a JSON Array in Java

In javascript we need to call the push function to add a item to an existent array. With java is the same, only that you need to declare a new array with the JSONArray class and add the items with add.

The JSONArray can convert a JSON text into a Java object. The toString method converts to JSON text, and that's what we are going to send to Javascript.

JSONArray jsonArray = new JSONArray();
jsonArray.add("item number 1");
jsonArray.add("item number 2");
// in javascript this is something like :
// ["item number 1","item number 2"]

Sending a JSON Array and a JSON Object to javascript with Cordova

Usually we will return a response from java to javascript with some of the following options:

String response = "myinformation";

PluginResult result = new PluginResult(PluginResult.Status.OK, response);

// or


In this case if you try to replace response with one of the previous created variables (item or jsonArray) you will obviously get an error. why ? Because you're sending a not string variable, remember that JSON is a string, we will simply use the toString method of the variables to convert it to JSON String notation :

JSONArray jsonArray = new JSONArray();
JSONObject item = new JSONObject();

// Send an object 
//or Send an array

Have fun !

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.