Executing server code via the Kii Cloud SDK for Android

Here is an example of executing server code via the Android SDK.

  • // Instantiate the endpoint.
    KiiServerCodeEntry entry = Kii.serverCodeEntry("main");
    
    try {
      // Set parameters.
      JSONObject rawArg = new JSONObject();
      rawArg.put("username", "name_of_my_friend");
      rawArg.put("password", "password_for_my_friend");
    
      // Create a container of the parameters.
      KiiServerCodeEntryArgument arg = KiiServerCodeEntryArgument
          .newArgument(rawArg);
    
      // Execute the server code.
      KiiServerCodeExecResult res = entry.execute(arg);
    
      // Parse the result.
      JSONObject returned = res.getReturnedValue();
      String newUser = returned.getString("returnedValue");
    
      // Get the number of the executed steps.
      int steps = res.getExecutedSteps();
    
      // Get the version of Node.js on which the server code was executed.
      KiiServerCodeEnvironmentVersion executedVersion = res.getEnvironmentVersion();
    
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    } catch (JSONException e) {
      // Handle the error.
    }
  • // Instantiate the endpoint.
    KiiServerCodeEntry entry = Kii.serverCodeEntry("main");
    
    // Set parameters.
    JSONObject rawArg = new JSONObject();
    try {
      rawArg.put("username", "name_of_my_friend2");
      rawArg.put("password", "password_for_my_friend");
    } catch (JSONException e) {
      // Handle the error.
    }
    
    // Create a container of the parameters.
    KiiServerCodeEntryArgument arg = KiiServerCodeEntryArgument
        .newArgument(rawArg);
    
    // Execute the server code.
    entry.execute(arg, new KiiServerCodeEntryCallback() {
      public void onExceuted(KiiServerCodeEntry entry, KiiServerCodeEntryArgument argument,
                             KiiServerCodeExecResult res, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
    
        try {
          // Parse the result.
          JSONObject returned = res.getReturnedValue();
          String newUser = returned.getString("returnedValue");
    
          // Get the number of the executed steps.
          int steps = res.getExecutedSteps();
    
          // Get the version of Node.js on which the server code was executed.
          KiiServerCodeEnvironmentVersion executedVersion = res.getEnvironmentVersion();
        } catch (JSONException e) {
          // Handle the error.
        }
      }
    });

Here is the brief explanation of the sample code:

  • Creates a KiiServerCodeEntry instance by executing the serverCodeEntry() method with the target endpoint (function) name. You can specify the version of the JavaScript engine for executing the server code with the KiiServerCodeEnvironmentVersion argument.
  • Prepares custom parameters (the username and password in this sample code) by setting them in a JSON Object and by creating a KiiServerCodeEntryArgument with the newArgument() method. Make sure to align the parameter keys with those in the server code so that the server code can properly get the parameter values (in this sample code the keys are "username" and "password").
  • Executes the server code by calling the execute() method with the custom parameters.
    • If there is a logged-in user, Kii Cloud interprets the server code as being executed by this user.
    • If there is no logged-in user, Kii Cloud interprets the server code as being executed by an anonymous user.
  • Gets the result by calling the getReturnedValue() method.
  • (Optionally) gets the number of the executed steps with the getExecutedSteps() method.
  • (Optionally) gets the version of Node.js with the getEnvironmentVersion() method.

The getReturnedValue() method will give you a JSON Object with the result stored in the "returnedValue" field (See Data types of return values for some examples). Use the appropriate method to fetch the actual result; for example, we fetch the newly-created username with getString() method in the sample code.

If your server code returns the value after the timeout, you can get the value with the getReturnedValue() method likewise. See Timeout for the format of the JSON object you can get with the method.