Think of a scenario that goes like this: at a central point, orders are being collected and will be prepared for transport. Goods need to be delivered and customers need to sign in the app once they receive the goods that they have ordered. Each driver has a mobile device and an app to support this process digitally.
This is the process for which we will provide the next three recipes and we will be using Parse for it, as it is the most suitable backend for the solution that we are going to create.
The upcoming recipe describes how to set up Parse, how to consume data from Parse into your Android app, and how to send data, such as a signature, from the app to Parse.
To go through this recipe, you will need Android Studio up and running and Internet access. That's all folks.
Let's create an app that connects to a Parse backend first so that we have a fundament on which we can build our app. Let's name our app CloudOrder
. The further steps are as follows:
CloudOrder
and enter packtpub.com
for the Company Domain field or any other name that suits you or your company best. Then, click on the Next button.CloudOrder
or something similar in the field displaying the hint about your app name. Pick a value for Company type that suits your situation best and depending on the chosen value, complete any of the other fields. Once this is done, hit the Start using Parse button. Select Data as the product you want to start using. Choose Mobile as your environment. Next, select a platform. Choose Android, and in the next view, choose the Native (Java) option.app
folder. Note that one of the underlying folders is called libs
.Parse-x.x.x.jar
file (where x.x.x
indicates the version number) into the libs
folder in Android Studio. If the Non-Project Files Access dialog box appears, just click on the OK button. When you do this, Parse-x.x.x.jar
will appear under the libs
folder.build.gradle
file in the apps
folder by double-clicking on it. In the dependencies section, we need to add two lines, so it will look like as shown in the following example. Just after the two lines that are already in there, add the dependencies for both the bolts
and parse
libraries:dependencies { compile 'com.android.support:appcompat-v7:22.0.0' compile 'com.parse.bolts:bolts-android:1.+' compile fileTree(dir: 'libs', include: 'Parse-*.jar') }
AndroidManifest.xml
file, add permissions needed to access the Internet. The Manifest
file will reside in the /app/src/main
folder. Double-click on it to open it. Add the permissions for both the Internet and to access the network state, as shown in the following example. Also, define the name for the package name
+ CloudOrderApplication
application:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.packtpub.cloudorder" > <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE" /> <application android:name="com.packtpub.cloudorder.CloudOrderApplication"
src/main/java/com.packt.cloudorder
folder. Right-click on this folder. In the context menu that pops up, choose New, and in the submenu, choose Java Class. In the dialog box that will be shown, enter CloudOrderApplication
as the content for the Name field. Then, click on the OK button.Application
class and override the onCreate
method. In the onCreate
method, right after super.OnCreate()
, add the initialization for Parse, as indicated by Parse using the following code:Parse.initialize(this, "your application Id", "your client Id");
gradle
file, your project needs to be synchronized. To do so, click on the button with the tooltip that reads Sync project with Gradle Files. You will find this on the navigation bar. Alternatively, you may also click on the Sync Now link.Parse
, you will notice that Android Studio suggests that this probably refers to com.parse.Parse
. Press Alt + Enter to accept this suggestion or just add the import com.parse.Parse
line yourself. Finally, your class will look like this:package com.packt.cloudorder; import android.app.Application; import com.parse.Parse; public class CloudOrderApplication extends Application{ @Override public void onCreate(){ super.onCreate(); Parse.enableLocalDatastore(this); Parse.initialize(this, "your application Id", "your client Id"); } }
MainActivity
file and add the following lines to your onCreate
method:ParseObject testObject = new ParseObject("CloudOrder"); testObject.put("customer", "Packt Publishing Ltd"); testObject.saveInBackground();
CloudOrder
class is sent to Parse and created at Parse.This is what the data section at www.Parse.com looks like:
If this test succeeds, remove the three lines that you have added to the onCreate
method of MainActivity
as we no longer need them.
Well done! You have just created your first Parse app! Let's move on and see how to extend the CloudOrder
app!
The Parse SDK will take care of retrieving or sending data. Using the ParseObject
class, Query
and other Parse classes' all data communication takes place automatically.
At www.parse.com, you will find additional information about caching policies, saving data to the cloud, and other interesting features.