Chapter 3. Creating and Accessing Content from the Cloud

In this chapter, we will learn how to consume content from the Web using our application; this content could be a list of items inside an XML or JSON file (something that we wish to display), retrieved from the Internet. For instance, if we were building an app that shows the current weather conditions, we would need to contact an external API to retrieve all the information needed.

We will create our own cloud database in Parse, a service that allows us to do this really quickly without the hassle of creating and maintaining our own servers. Apart from this, we will populate the database with information to be displayed in MasteringAndroidApp.

We will also cover best practices regarding network requests with Google Volley, using the ultrafast HTTP library, OkHttp, and parsing the requested objects efficiently with Gson. We will cover the following topics in this chapter:

  • Creating your own cloud database
  • Consuming content from Parse
  • Google Volley and OkHttp
  • Parsing objects with Gson

Creating your own cloud database

At this stage of the project, we have to start modeling our own version of MasteringAndroidApp. Feel free to develop your own ideas and use the database for your own data. Follow this example as a guide; you don't necessarily have to copy all the lines of code exactly as I write them. In fact, if you develop your own example at the end of this book, you will have something that you can use. For instance, you can create an app for your own personal use, such as a task reminder, travel diary, personal photo gallery—or anything else that is suitable for storage in the cloud.

You could also try to monetize this app; in this case, you should try to develop something interesting for users. For instance, it can be a news feed reader or recipes reader for food; it can be any app where you can submit content to the cloud and notify users that new content is available.

During this process, we will explain the importance of the Application class, which is used to set up Parse in our project.

Parse

Parse is free if you have less than 30 requests per second. I imagine that if you have enough users requesting information from your app 30 times per second, which is 1,800 per minute, you can surely afford to upgrade to a paid account or even build your own server! This service is a very easy and reliable way to have the server side covered for your app. It also provides a push notifications service and analytics, that's another point in favor.

We will proceed with creating a new account; after this, we need to name our application in Parse. Here, I will use MasteringAndroid. Once you name the application, you will be on the main page of your account. We need to navigate to Data Service | Mobile | Android | Native Java.

The following image shows the data services as a cloud:

Parse

Adding the Parse SDK to our project

To access the data service from our app, we need to install the Parse SDK (System Development Kit). For this, Parse refers us to a quick start guide, which contains all of the code, including the API Keys for our application, that is ready to be copied and pasted into our project.

Basically, we need to complete two steps:

  1. The first one is to download a .jar library file that we need to copy into the libs folder in our project. After copying it, we need to tell our build system to include this library in our application. To do this, we need to find the build.gradle file inside our Application folder (be careful, there are two build.gradle files in our project) and add the following lines:
    dependencies {
      compile 'com.parse.bolts:bolts-android:1.+'
      compile fileTree(dir: 'libs', include: 'Parse-*.jar')
    }
  2. In the following image, you can see the two files named build.gradle; the one that is selected is the right one:
    Adding the Parse SDK to our project
  3. The second step is to initialize the Parse SDK in our project; for this, we can navigate directly to https://www.parse.com/apps/quickstart?app_id=masteringandroidapp. Replace your own app ID in the link or find the link by clicking on your home page, as in the following screenshot:
    Adding the Parse SDK to our project
  4. After clicking on quickstart guide, go to Data | Mobile | Android | Native | Existing Project.
  5. It will ask you to add the INTERNET and ACCESS_NETWORK_STATE permissions to your AndroidManifest.xml file if they are not already added.

Android's Application class

The next thing we can take note of is that we need to add the code to initialize Parse to our Application class; however, our Application class is not created by default in our project. We need to create and understand what the Application class is and how it works.

To create an Application class, we will right-click on our package and create a new Java class called MAApplication extending Application. Once this extends Application, we can override the onCreate method. Then, we will right-click inside our class | Generate. | Override Methods | onCreate.

This will override the onCreate method, and we will be ready to implement our own functionality there. The onCreate method is called every time our Application is created; therefore, it's the right place to initialize our libraries and third-party SDKs. Now, you can copy and paste the Parse initialization lines as seen in quick start guide:

Tip

Be careful, this is unique, and for your own account you should have your own keys.

Parse.initialize(this, "yourKeyHere", "yourKeyHere");

To finish, we need to tell our app that we have a new Application class and that that's the one we want to use; if we don't do this, our Application class won't be recognized and onCreate won't be called.

In our manifest, inside the <application> tag, we need to set the attribute name to match our own application. Execute the following code:

<application
    android:name="MApplication "
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_newname"
>

The Application class encapsulates everything in our app; the activities are contained in the application, and subsequently, the fragments are contained in the Activities. If we need a global variable in our app that needs to be accessed by all Activities/Fragments, this would be the right place to have it. In the next chapter, we will see how we can create this global variable. The following diagram is the graphic structure of an app:

Android's Application class

Creating the database

As we know, the example that we will create during this book is an app that will have Android-related job offers; therefore, we need to create a database to store these job offers.

The database can be changed during development (this will be more difficult to do when the app is released and has users). However, for now we will look at the big picture, creating the whole system rather than having a final version of the database with all the fields completed.

To create a table, click on the Core section as shown in the following screenshot:

Creating the database

First, create a table by clicking on the + Add Class button and call it JobOffer with the following attributes, which can be added by clicking on the Col+ button:

  • objectId: This is created by default: String
  • title: This is the job title: String
  • description: This is the job description: String
  • salary: This indicates the salary or daily rate: String
  • company: This indicates the company offering the job: String
  • type: This indicated the type of employee, which is permanent, contract, or freelancer: String
  • imageLink: This is the image of the company: String.
  • Location: This indicates the location of the job: String
  • createdAt , updatedAt: This is the date of the job; the columns are created with a default date

To add data to the tables, select the table on the left and click on + Row. We only need to complete the columns that we created; the columns created by default, such as the ID or date, will be completed automatically. So far, our table should look as follows:

Creating the database

Feel free to add more details, such as the contact person, e-mail, and mobile number. You could also add more tables; for instance, a new JobType table containing the type of job and the field type instead of String would be Relation<JobType>.

We have what we need for our example; the next thing to do is consume this data using our app.

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

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