Code

The ClearBlade Java API allows executing a Code Service on the platform from your Java application.

Please make sure that you have initialized and authenticated with the ClearBlade platform prior to using the Code API.

You need to import the following packages to use the Code API:
import com.clearblade.java.api.Code;
import com.clearblade.java.api.CodeCallback;

Code Service Without Parameters

A code service which does not take any parameters can be executed as follows:

String serviceName = "yourServiceName";

CodeCallback codeCallback = new CodeCallback() {
	@Override
	public void done(JsonObject response) {
	    // Code Service executed successfully
		Log.i("codeResponse", response.toString());
	}

	@Override
	public void error(ClearBladeException exception) {
	    // Code Service execution failed
		Log.i("codeResponse", exception.getMessage());
	}
};

Code codeService = new Code(serviceName);
codeService.executeWithoutParams(codeCallback);

Code Service With Parameters

A Json Object of parameters needs to be passes to the Code class constructor along with the service name:

String serviceName = "yourServiceName";
String parameters = "{\"param1\":\"value1\"}";
JsonObject parameterJsonObject = new JsonParser().parse(parameters).getAsJsonObject();

CodeCallback codeCallback = new CodeCallback() {
	@Override
	public void done(JsonObject response) {
	    // Code Service executed successfully
		Log.i("codeResponse", response.toString());
	}

	@Override
	public void error(ClearBladeException exception) {
	    // Code Service execution failed
		Log.i("codeResponse", exception.getMessage());
	}
};

Code codeService = new Code(serviceName, parameterJsonObject);
codeService.executeWithParams(codeCallback);