So far I’ve been showing how to access a web service from a simple app with no significant features. Now I’ll show a more integrated version, using a more complete app that I created for a course on working with persistent data in Android apps.
- [Narrator] In all of my demonstrations so far in this course, I've been using a very simple app with no significant features. Now I'll move some of those capabilities, accessing a web service into a more complete application. This is the naughtiest restaurant application. I built this application as part of a course on working with persistent data on Adroid apps. In the current version of the application, all of the data is being provided by a java class and then stored in an SQLite database on the device.
So the data you see in this list is coming from SQLite and the images are also packaged with the application. My goal now is to take some of the features that I described, the ability to use http url connection and other related classes to get this data over the web. In this version of the application, I've copied over a number of files that I create earlier. I have my intent service class, and I've registered that class in the application manifest, right down here.
I've also included my http helper and my network helper classes. This all has exactly the same code that I used in previous exercises. So now my goal is to integrate these tools and make the request from the main activity of this class. As before, I've declared the end point of my web service as a constant in the main activity. And I've also set up a broadcast receiver and I'm registering that receiver in the On Create method down at the bottom and un-registering it in an On Destroy method.
Right now, in the application, there's an array list named mItem list and it's being filled in at run time by a request from the database. I'm going to change that. First, I'm going to disable the database completely. I'll comment out this field, named mDataSource and that will create a number of problems with the code. So I can then press the F2 key a number of times and find all the references to that code. I'll comment out these three lines. Then I'll comment out this line and I'll also comment out the Open and Close method calls.
I'll press F2 one more time and make sure that I don't have any other references to that data source. That completely cuts off my access to the SQLite database in this activity. Next, I'll go to my broadcast receiver reference. Here, I'm receiving the data from the back end intense service. I'm receiving it as an array of data item objects. I need to turn it back into an array list, or really a java list. And I'll do that with this bit of code. I'll say mItemList equals arrays.aslist and then I'll pass in my array.
So I'm taking a raw array and wrapping it in a complex list of objects. Next, I need to instantiate my item adapter. I'm displaying the data using a Recycler view and with Recycler views, you always use an adapter. My adapter is defined in this class, named DataItemAdapter and in my Main Activity, I've already declared an instance of that class. I'll use the Find Usages tool to locate the call that actually instantiates the adapter and passes it to the Recycler view.
It's in the Method Display Items. So to connect everything together, I'll come back to my broadcast receiver and I'll call that method. And for the moment, I'll pass in a value of null. That argument is for filtering on a category. We'll get to that later. So now if whenever the data comes back from the service, I'm displaying it in the list. Next, I need to make the request to the back end service. I'll go back down to my On Create method and I'll place the cursor right down here at the bottom of the method. I'm going to comment out this call to display data items because when the On Create method is called, the data won't be available yet.
And instead, I'll check for my network capability first. With networkok equals networkhelper.hasNetworkAccess and I'll pass in this as the context. And then if the network is okay, I'll make the request. As I did in an earlier exercise, I'll create an intent object that says I'm launching the service. Then, I'll call intent.setdata and I'll pass in uri.parse and then I'll pass in the constant that represents my end point, JSON_URL.
And finally, I'll start the service. As I did previously, if I don't have network connectivity, I'll display a toast message. Finally, I'll come down here to the Display Data Items method. I only want to execute this code if I have valid data so I'm just going to use a code template, inn for if not null. And I'll look at the mItemList field and then I'll move all of this code into that conditional blog. So now let's track all the code. As the application starts up, in the On Create method, I'm executing a call to the service and I'm registering my listener.
And I'm registering my listener. When the data comes back from the back end service, it'll be displayed in the Recycler view using this code. And so let's see how it works now. Just as before, the data is retrieved and displayed but now, instead of retrieving it from a local SQLite database, I'm retrieving it from the remote web service.
IInstructor David Gassner starts with an overview discussion about available networking strategies, and then dives into how to declare networking permissions, make network requests, handle responses, parse data returned from a web service, and pass user credentials to services that support HTTP basic authentication. He also provides an overview of popular higher-level networking libraries, including OkHttp and Picasso, and offers recommendations for improving performance in network-connected apps. David also covers using static feeds, using dynamic PHP pages hosted on a public server web service, and integrating apps with network data storage.
- Integrating mobile apps
- Communicating with Android
- Preventing app freezes with background threads
- Preparing an app for network communication
- Declaring permissions
- Checking network connectivity
- Retrieving data over the web
- Using traditional APIs
- Using third-party libraries