How to create and deploy your Parse Cloud Code
Introduction
For complex apps, sometimes you need a bit of logic that isn’t running on the mobile device. Cloud Code makes it possible.
Cloud Code is built on the same JavaScript SDK that powers thousands of apps. The only
difference is that this code runs in your Parse Server rather than running on the user’s
mobile device. When you update the Cloud Code, it becomes available to all mobile environments
instantly and you don’t have to wait until a new release of your application comes up. This lets you change
app behavior on the fly and also lets you add new features on your app faster.
This section explains how to create and deploy Cloud Code, followed by how to call a cloud function
in Android projects through Back4App.
Even if you’re only familiar with mobile development, we hope you’ll find Cloud Code
straightforward and easy to use.
You can find more in-depth information in Parse Official Cloud Code Documentation.
Prerequisites
To complete this tutorial, you need:
- Android Studio.
- An app created at Back4App.
- Note: Follow the New Parse App tutorial to learn how to create a Parse App at Back4App.
- An Android app connected to Back4app.
- Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
- A device (or virtual device) running Android 4.0 (Ice Cream Sandwich) or newer.
Step 1 - Create a Cloud Code File
Create a new file and name it main.js
and add the following Parse.Cloud.define
function, which has its name and a callback as arguments.
You can pass parameters to your Cloud function from your Android App and access then within the
request.params
object.
1
2
3
4
5
6
7
Parse.Cloud.define("test", (request) => {
var text = "hello world";
var jsonObject = {
"answer": text
};
return jsonObject
});
1
2
3
4
5
6
7
Parse.Cloud.define("test", function(request, response) {
var text = "hello world";
var jsonObject = {
"answer": text
};
response.success(jsonObject);
});
Step 2 - Upload to Cloud Code
- Go to your App at Back4App website and click on
Dashboard
. - Find the
Cloud Code
and click onFunctions & Web Hosting
. It looks like this: - Upload or create a new file (you can also edit the current
main.js
file directly on the browser). Then, click atDeploy
as shown here:
Step 3 - Add Android Code
Import the following dependecies:
1
2
3
4
5
6
7
8
9
// Front End Dependencies
import android.widget.Toast;
// Parse Dependencies
import com.parse.FunctionCallback;
import com.parse.ParseCloud;
import com.parse.ParseException;
// Java Dependencies
import java.util.HashMap;
import java.util.Map;
To call your Cloud Code function, you need to call a special android function: ParseCloud.callFunctionInBackground
.
Its first parameter is the function name on Cloud Code and the second one is the HashMap
that has every parameter that will be passed to the function. The third argument is the callback
that will be executed after the function has been called.
The following code calls the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Use this map to send parameters to your Cloud Code function
// Just push the parameters you want into it
Map<String, String> parameters = new HashMap<String, String>();
// This calls the function in the Cloud Code
ParseCloud.callFunctionInBackground("test", parameters, new FunctionCallback<Map<String, Object>>() {
@Override
public void done(Map<String, Object> mapObject, ParseException e) {
if (e == null) {
// Everything is alright
Toast.makeText(MainActivity.this, "Answer = " + mapObject.get("answer").toString(), Toast.LENGTH_LONG).show();
}
else {
// Something went wrong
}
}
});
In this function, the
mapObject
has a key calledanswer
, which contains the value hello world,
which will be printed on the screen by theToast
class when the code is executed.
It’s done!
At this stage, you are able to code and call your own Cloud Code in your Android App using Parse Server Core features through Back4App!