Android
Cloud Code Functions
9 min
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 to complete this tutorial, we need android studio an app created on back4app note follow the new parse app tutorial to learn how to create a parse app on back4app an android app connected to back4app note follow the install parse sdk tutoria l to create an android studio project connected to back4app a device (or virtual device ) running android 4 0 (ice cream sandwich) or newer 1 create a cloud code file create a new file and name it main js main js and add the following parse cloud define 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 request params object parse server 3 x //main js 1 parse cloud define("test", (request) => { 2 var text = "hello world"; 3 var jsonobject = { 4 "answer" text 5 }; 6 return jsonobject 7 }); parse server 2 x //main js 1 parse cloud define("test", function(request, response) { 2 var text = "hello world"; 3 var jsonobject = { 4 "answer" text 5 }; 6 response success(jsonobject); 7 }); 2 upload to cloud code go to your app at back4app website and click on dashboard dashboard find the cloud code cloud code and click on functions & web hosting functions & web hosting it looks like this 3\ upload or create a new file (you can also edit the current main js main js file directly on the browser) then, click at deploy deploy as shown here 3 add android code import the following dependecies to call your cloud code function, you need to call a special android function parsecloud callfunctioninbackground 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 // use this map to send parameters to your cloud code function 2 // just push the parameters you want into it 3 map\<string, string> parameters = new hashmap\<string, string>(); 4 5 // this calls the function in the cloud code 6 parsecloud callfunctioninbackground("test", parameters, new functioncallback\<map\<string, object>>() { 7 @override 8 public void done(map\<string, object> mapobject, parseexception e) { 9 if (e == null) { 10 // everything is alright 11 toast maketext(mainactivity this, "answer = " + mapobject get("answer") tostring(), toast length long) show(); 12 } 13 else { 14 // something went wrong 15 } 16 } 17 }); in this function, the mapobject has a key called answer, which contains the value hello world, which will be printed on the screen by the toast 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!