0:00:00.123,0:00:06.268 [MUSIC] 0:00:06.268,0:00:09.883 (University of Maryland) 0:00:10.854,0:00:14.625 (Programming Mobile Applications for[br]Android Handheld Systems) 0:00:15.639,0:00:20.854 One of the defining characteristics of [br]modern hand held systems is that they can 0:00:20.854,0:00:26.266 keep us connected and networked without [br]tethering us to a single location. 0:00:27.453,0:00:30.400 In this lesson, we'll explore [br]the software and 0:00:30.420,0:00:36.254 programming practices that you'll need to [br]connect your applications to the network. 0:00:37.091,0:00:40.707 So I'll start this lesson by discussing [br]networking in general. 0:00:41.262,0:00:46.329 That discussion will focus on connecting [br]your applications to the internet using 0:00:46.329,0:00:55.587 the Hypertext Transfer Protocol or HTTP [br]specifically by using HTTP GET requests. 0:00:56.827,0:00:59.815 After that, I'll present [br]several classes that 0:00:59.815,0:01:03.075 Android provides to support [br]this kind of networking. 0:01:04.005,0:01:07.623 And lastly, I'll discuss how your [br]applications can process the 0:01:07.623,0:01:12.295 data they receive in response [br]to these HTTP GET requests. 0:01:12.616,0:01:17.148 In particular, I'll talk about two [br]popular data formatting languages. 0:01:17.362,0:01:22.365 One, the Javascript [br]Object Notation Language, or JSON. 0:01:22.880,0:01:27.602 And two, the Extensible Markup Language, [br]or XML. 0:01:28.248,0:01:31.712 And I'll talk about how you parse, [br]or make sense, of 0:01:31.712,0:01:35.832 these HTTP responses when they're [br]formatted in one of these languages. 0:01:37.906,0:01:41.104 So early handheld devices [br]gave us mobility. 0:01:41.337,0:01:46.444 You could move from one place to another, [br]and still perform useful computation. 0:01:47.161,0:01:48.029 However, 0:01:48.029,0:01:51.823 their networking capabilities [br]were primitive by today's standards. 0:01:52.755,0:01:57.380 Now moving forward, today's devices [br]combine powerful processors 0:01:57.380,0:02:02.617 with fast network connections [br]over WiFi and cellular networks. 0:02:03.495,0:02:07.530 Handheld applications will therefore [br]often want to make use 0:02:08.125,0:02:13.138 of these networking capabilities to [br]access and provide data, and services. [br] 0:02:13.138,0:02:18.244 Now, to help you do this, Android includes 0:02:18.244,0:02:24.831 a variety of networking support classes, [br]including the Socket and URL classes, 0:02:24.841,0:02:26.903 in the Java.net packages. 0:02:27.561,0:02:35.757 The HttpRequest and HttpResponse classes, [br]in the org.apache packages. 0:02:36.616,0:02:42.451 And the URI, AndroidHttpClient,[br]and AudioStream classes, 0:02:42.743,0:02:44.998 in the android.net packages. 0:02:46.166,0:02:50.037 In this lesson we're going to look at [br]several of these classes, 0:02:50.400,0:02:54.956 using each of them to implement [br]the same example application. 0:02:55.590,0:02:58.869 And this application interacts [br]with an internet service 0:02:59.226,0:03:02.835 to get information about [br]earthquakes that have occurred 0:03:02.835,0:03:05.589 in a particular geographic region. 0:03:06.213,0:03:10.519 And as you'll see, that data is returned [br]in various formats. 0:03:10.868,0:03:14.808 Now initially we'll just display the [br]downloaded text as is. 0:03:15.518,0:03:19.128 Later on in the lesson, I'll show you [br]how to process that data 0:03:19.485,0:03:22.884 to extract [br]just the information that you want. 0:03:23.527,0:03:25.294 Oh, and, and one other thing. 0:03:25.599,0:03:26.888 As you'll see in a second, 0:03:27.101,0:03:30.427 because this data includes [br]geographic information, 0:03:30.801,0:03:35.672 it's really begging to be displayed [br]on a map, rather than as text. 0:03:35.672,0:03:39.737 Now, we won't do that in this lesson, [br]but keep this in mind, 0:03:39.737,0:03:44.728 because we'll come back to this when we [br]get to the lesson on maps and location. 0:03:45.744,0:03:49.816 So in order to make this application [br]work, the code needs to create 0:03:49.816,0:03:55.951 an HTTP request, send it to a [br]server computer, retrieve the results, 0:03:56.335,0:04:00.988 and then display those results. [br]Android provides several classes for 0:04:00.988,0:04:05.736 helping with this. [br]Three we'll talk about now are 0:04:05.736,0:04:11.623 the Socket class, the HttpUrlConnection [br]class 0:04:12.023,0:04:15.189 and the AndroidHttpClient. 0:04:20.490,0:04:23.910 I'll launch the networking [br]socket application. 0:04:25.277,0:04:26.363 As you can see, 0:04:26.628,0:04:31.760 this application initially displays [br]a single button labeled LoadData. 0:04:32.674,0:04:36.106 When I press that button, [br]the application will issue 0:04:36.106,0:04:40.573 an HTTP GET request to an [br]external server. 0:04:41.076,0:04:44.978 And that server will respond [br]with some complex text 0:04:45.388,0:04:48.064 containing the requested [br]earthquake data. 0:04:49.187,0:04:50.008 Okay. 0:04:50.398,0:04:57.331 So now I'll press the Load Data button [br]and there you can see the requested data. 0:04:58.601,0:05:02.942 Let's look at the source code [br]to see what it took to get that data. 0:05:03.283,0:05:05.797 Now here I've opened the application [br]in the IDE. 0:05:08.259,0:05:11.297 Now I'll open the main activity [br]for this application. 0:05:13.376,0:05:16.774 And here I'm showing the listener [br]for the Load Data button. 0:05:18.684,0:05:23.775 When this button is pressed, the [br]application will create, and then execute, 0:05:23.775,0:05:30.365 an AsyncTask called HttpGetTask.[br]Let's look at that class. 0:05:31.725,0:05:36.855 The HttpGetTask class first declares [br]some variables, 0:05:37.040,0:05:41.141 they're used in creating [br]an HTTP GET Request. 0:05:42.917,0:05:47.730 When the execute method is called, on[br]the HttpGetTask, 0:05:48.480,0:05:50.670 the doInBackground method is called. 0:05:51.986,0:05:54.975 And that method begins [br]by creating a new socket 0:05:55.611,0:06:02.004 that will be connected to [br]the host computer, api.geonames.org, 0:06:02.858,0:06:08.664 on the standard http port, port 80.[br]Next, the code gets 0:06:08.664,0:06:14.137 the socket's output stream, [br]and then writes the HTTPGETCOMMAND. 0:06:15.101,0:06:19.846 And this string will be sent to [br]the host computer, which interprets it 0:06:19.846,0:06:23.993 as an HTTPGetRequest, and then responds 0:06:23.993,0:06:27.441 by sending back [br]the appropriate response data. 0:06:28.593,0:06:32.927 And then this code continues [br]by getting the socket's input stream 0:06:33.723,0:06:37.235 And by passing it to a method [br]called readStream. 0:06:38.629,0:06:42.508 The readStream method ultimately [br]reads the response data 0:06:42.761,0:06:48.854 from the socket's InputStream and then [br]returns the response as a single string. 0:06:50.385,0:06:52.424 And this string is passed to the 0:06:52.424,0:06:57.692 onPostExecute method which executes [br]on the main thread 0:06:58.535,0:07:01.754 and which displays the response [br]in the text view. 0:07:02.485,0:07:07.170 If we return back to the application, [br]you'll notice that the response text 0:07:07.488,0:07:13.005 includes not only the earthquake data, [br]but also the HTTP response headers. 0:07:13.904,0:07:17.554 Now normally, I wouldn't want [br]to display that text here. 0:07:17.942,0:07:20.365 I really just want to show you [br]the earthquake data. 0:07:20.777,0:07:24.162 So in the case, I should have [br]parsed the response 0:07:24.517,0:07:26.858 and pulled out [br]just the data that I wanted. 0:07:28.528,0:07:31.665 In addition, you might have noticed [br]I didn't write any of the 0:07:31.665,0:07:36.566 error handling code that you'd really [br]need to make this application robust. 0:07:37.584,0:07:42.442 And these points capture pretty well [br]the trade-offs of using sockets. 0:07:42.442,0:07:46.372 They're very low level: you can write [br]whatever you want on the socket, 0:07:46.981,0:07:50.904 but in return, you have to handle [br]all the many details 0:07:50.908,0:07:54.855 of making the HTTP requests, [br]all the error handling, 0:07:55.197,0:07:58.642 and all the processing [br]of the HTTP responses. 0:08:02.160,0:08:08.265 The next implementation we'll look at [br]uses the HttpUrlConnection class. 0:08:08.904,0:08:12.801 This class provides a higher-level [br]interface that handles more 0:08:12.801,0:08:17.034 of the networking details [br]than the socket class does. 0:08:17.630,0:08:21.463 But as we'll see in a moment, [br]it also has a less flexible API 0:08:21.911,0:08:27.566 than our last option, [br]the Android HTTP client class. 0:08:28.480,0:08:32.122 Now, having said that, I'll also [br]point out that the Android team 0:08:32.122,0:08:36.959 is not actively working on the [br]Android HTTP client anymore. 0:08:36.959,0:08:41.535 And is putting its efforts into [br]improving this class going forward. 0:08:42.032,0:08:45.052 So let's look at the example application 0:08:45.052,0:08:50.687 implemented this time [br]with the HttpURLConnection class. 0:08:51.476,0:08:52.236 ... 0:08:52.236,0:08:53.075 ... 0:08:53.075,0:08:56.616 Now I'll launch [br]the NetworkingURL application. 0:08:57.279,0:09:00.201 As before, this application [br]initially displays 0:09:00.201,0:09:02.884 a single button labeled Load Data. 0:09:04.115,0:09:08.498 And as before, when I press on [br]that button the application will issue 0:09:08.498,0:09:15.328 an HTTP GET request to an external server, [br]and that server will respond 0:09:15.328,0:09:19.763 with some complex text, containing [br]the requested earthquake data. 0:09:20.314,0:09:22.223 Okay, so now I'll press the 0:09:22.223,0:09:23.550 Load Data button. 0:09:24.622,0:09:28.805 There you can see the requested data, [br]appearing in a text view. 0:09:29.630,0:09:34.757 Notice, however, that this time, the HTTP [br]response headers have been stripped out. 0:09:36.524,0:09:39.095 Let's look at the source code [br]and see how this works. 0:09:41.149,0:09:44.237 Now, here I've got the application [br]opened in the IDE. 0:09:45.167,0:09:48.749 Now, I'll open the main activity [br]for this application. 0:09:49.756,0:09:53.156 And here, I'm showing the listener [br]for the load data button. 0:09:54.382,0:09:59.285 As before, when this button is pressed, [br]the application will create and then 0:09:59.285,0:10:06.749 execute an AsyncTask called, HttpGetTask. [br]Let's look at that class. 0:10:08.706,0:10:13.942 When the execute method is called on [br]HTTPGetTask, the doInBackground method 0:10:13.942,0:10:19.120 is invoked. That method begins [br]by creating a new URL object, 0:10:19.717,0:10:25.087 and passing a URL string [br]for the desired service as a parameter. 0:10:26.480,0:10:29.475 The code then calls the [br]open connection method 0:10:29.475,0:10:35.251 on the URL object, which returns [br]an httpUrlConnection. 0:10:35.868,0:10:41.115 This object is then stored in a variable [br]called HttpURLConnection. 0:10:42.394,0:10:47.998 The code continues by getting the [br]HttpURLConnection's input stream, 0:10:48.562,0:10:51.407 and by passing it through [br]the readStream method. 0:10:52.440,0:10:56.726 And as before, the readStream method [br]reads the response data 0:10:56.726,0:11:00.842 from the socket's input stream, [br]and then returns the response, 0:11:01.132,0:11:06.663 as a single string. [br]This time however, the HTTP URL connection 0:11:07.036,0:11:13.780 strips off the HTTP response headers [br]and handles the error checking for you. 0:11:14.888,0:11:18.810 Now this string is then passed [br]to the onPostExecute method 0:11:19.674,0:11:22.797 which displays the response [br]in a text view. 0:11:24.772,0:11:31.674 The third class is Android HTTP client.[br]This class is an implementation of the 0:11:31.674,0:11:38.783 Apache project's DefaultHttpClient and [br]it allows a great deal of customization. 0:11:39.184,0:11:44.388 In particular, the class breaks [br]an HTTP transaction into 0:11:44.978,0:11:50.613 a request object and into a response object. [br]So you can create subclasses 0:11:50.613,0:11:55.092 that customize the handling of requests [br]and their responses. 0:11:55.517,0:11:58.697 Now, by this point, you know [br]what the application looks like, 0:11:58.928,0:12:02.712 so let's jump straight into the code [br]and look at the implementation. 0:12:05.050,0:12:11.607 Now, here I've got the networking Android [br]HTTP client application opened in the IDE. 0:12:12.753,0:12:16.439 Now, I'll open the main activity [br]for this application. 0:12:17.045,0:12:20.786 And let's go right to the HTTP get task class. 0:12:22.160,0:12:24.415 That class begins by creating a new 0:12:24.415,0:12:31.193 AndroidHttpClient object by calling [br]the classes newInstance method. 0:12:32.340,0:12:36.352 Now, when the doInBackground method [br]is called, the code creates 0:12:36.352,0:12:42.775 an HttpGet object, passing in [br]the URL string for that request. 0:12:43.484,0:12:46.691 Next, it creates a [br]ResponseHandler object. 0:12:47.105,0:12:53.896 This object is responsible for handling [br]the response to the HttpGet request. 0:12:53.896,0:12:57.508 In this case, the ResponseHandler [br]is of type 0:12:57.508,0:13:02.446 basicResponseHandler, [br]which will return the response's body. 0:13:03.288,0:13:07.580 Now we'll see a more complex [br]ResponseHandler later in this lesson. 0:13:08.919,0:13:12.387 And finally, the request and the ResponseHandler 0:13:12.667,0:13:15.066 are passed into the execute method, 0:13:15.457,0:13:18.927 which sends the request, [br]gets the response, 0:13:19.824,0:13:21.968 passing it through the ResponseHandler. 0:13:23.014,0:13:24.702 And the result of all this 0:13:24.702,0:13:28.570 is then passed on to onPostExecute, 0:13:28.971,0:13:31.731 which displays the response [br]in a text field. 0:13:31.731,0:13:36.246 [BLANK_AUDIO]