Sample code for 30+ languages & platforms
Android™

Call a JavaScript Function Returning an Array

See more JavaScript Examples

Demonstrates how to call a JavaScript function that returns an array.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    // ----------------------------------------------------------------------------------
    // The Javascript functions called in this example are shown at the bottom of this page.
    // -----------------------------------------------------------------------------------

    CkStringBuilder sbScript = new CkStringBuilder();
    success = sbScript.LoadFile("js_function_returning_array.js","utf-8");
    if (success == false) {
        Log.i(TAG, sbScript.lastErrorText());
        return;
        }

    CkJs js = new CkJs();

    CkJsonObject result = new CkJsonObject();
    result.put_EmitCompact(false);

    // Call Eval to add the function to the context's global object
    success = js.Eval(sbScript,result);
    if (success == false) {
        // Examine the result for an exception.
        Log.i(TAG, result.emit());

        // Also examine the LastErrorText.
        Log.i(TAG, js.lastErrorText());
        return;
        }

    // ------------------------------------------------------------------------------
    // Call each function

    CkJsonObject funcCall = new CkJsonObject();

    // Create JSON specifying the function name and arguments
    // The function has no arguments, so we only specify the name.

    funcCall.UpdateString("name","getDays");

    success = js.CallFunction(funcCall,result);
    if (success == false) {
        // Examine the result for an exception.
        Log.i(TAG, result.emit());

        // Also examine the LastErrorText.
        Log.i(TAG, js.lastErrorText());
        return;
        }

    Log.i(TAG, result.emit());

    // Output:
    // {
    //   "type": "array",
    //   "value": [
    //     "Monday",
    //     "Tuesday",
    //     "Wednesday",
    //     "Thursday",
    //     "Friday"
    //   ]
    // }

    // Access each array value..
    int count = result.SizeOfArray("value");
    int i = 0;
    while (i < count) {
        result.put_I(i);
        Log.i(TAG, result.stringOf("value[i]"));
        i = i + 1;
        }

    // ------------------------------------------------------------------------------
    // Call the getRange(start,end) function

    funcCall.Clear();
    funcCall.UpdateString("name","getRange");
    funcCall.UpdateInt("args[0]",14);
    funcCall.UpdateInt("args[1]",21);
    success = js.CallFunction(funcCall,result);
    Log.i(TAG, result.emit());

    // Output:
    // {
    //   "type": "array",
    //   "value": [
    //     14,
    //     15,
    //     16,
    //     17,
    //     18,
    //     19,
    //     20,
    //     21
    //   ]
    // }

    count = result.SizeOfArray("value");
    i = 0;
    while (i < count) {
        result.put_I(i);
        Log.i(TAG, String.valueOf(result.IntOf("value[i]")));
        i = i + 1;
        }

    // ------------------------------------------------------------------------------
    // Call the getEmployees() function

    funcCall.Clear();
    funcCall.UpdateString("name","getEmployees");
    success = js.CallFunction(funcCall,result);
    Log.i(TAG, result.emit());

    // Output:
    // {
    //   "type": "array",
    //   "value": [
    //     {
    //       "id": 101,
    //       "name": "Alice",
    //       "role": "Dev"
    //     },
    //     {
    //       "id": 102,
    //       "name": "Bob",
    //       "role": "Manager"
    //     }
    //   ]
    // }

    count = result.SizeOfArray("value");
    i = 0;
    while (i < count) {
        result.put_I(i);
        Log.i(TAG, "name: " + result.stringOf("value[i].name"));
        Log.i(TAG, "role: " + result.stringOf("value[i].role"));
        Log.i(TAG, "id: " + String.valueOf(result.IntOf("value[i].id")));
        Log.i(TAG, "");
        i = i + 1;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}