Setting up Parse

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.

Getting ready

To go through this recipe, you will need Android Studio up and running and Internet access. That's all folks.

How to do it...

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:

  1. Start Android Studio and start a new Android Studio Project. Name your application 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.
  2. Select the Phone and Tablet option and optionally change the minimum SDK field. In my case, this will be API 14 (Android 4.x), which at the time of writing is the best choice to both reach an audience as large as possible and to benefit from the SDK functionality that we need. Make sure you will be targeting at least API level 9 as Parse does not support lower levels than this one. Click on the Next button to continue.
  3. Next, select Blank activity and click on the Next button. On the next page, just click on the Finish button. Your new project will be set up by Android Studio.
  4. Now, let's go to www.parse.com to create a new account and an app. Sign up with www.Parse.com. Enter your name, e-mail address, and chosen password, and then click on the Sign up button.
  5. The next page on www.Parse.com is the Get Started page. Enter 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.
  6. Choose the Existing project option. We are creating a new project; however, to know what is going on here, we will do the following things ourselves.
  7. Now, download the SDK. While downloading, switch to Android Studio and change the project view perspective from Android to Project. Then, expand the app folder. Note that one of the underlying folders is called libs.
  8. Drag and drop the 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.
  9. As we saw in Chapter 1, Welcome to Android Studio, we need to tell Gradle about this Parse library. Open the 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')
    }

    Note

    Instead of using the local JAR file, as described through step 6 to 8, we could also use a dependency like this:

    dependencies {
    …
        compile 'com.parse:android:1.8.2'}
  10. In the 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"
  11. Select and expand the 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.
  12. Make the new class a descendant of the 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");
  13. Android Studio is not happy yet. You will notice that the Parse part in your code is highlighted in red in the Android Studio IDE. This is because your app is not aware of this class. Any time you change a 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.
    How to do it...
  14. After this synchronization, Android Studio will have a clue about the Parse class, but you still need to add an import clause for that. If you hover over the part of your code that reads 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");
        }
    }
  15. We are almost done configuring our Parse-based app. Open the MainActivity file and add the following lines to your onCreate method:
    ParseObject testObject = new ParseObject("CloudOrder");
    testObject.put("customer", "Packt Publishing Ltd");
    testObject.saveInBackground();
  16. Don't forget to add the appropriate import statement. Run your app. If everything is set up successfully, a new object of the CloudOrder class is sent to Parse and created at Parse.
  17. On the parse web page, click on the Core button at the top of the navigation bar. Have a look at the Data section on the left-hand side of web page. CloudOrder should appear there, and if you click on it, you will see the entry (row), containing the properties (fields) that you just sent.

    This is what the data section at www.Parse.com looks like:

    How to do it...

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!

How it works...

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.

There's more...

At www.parse.com, you will find additional information about caching policies, saving data to the cloud, and other interesting features.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset