WEBVTT 00:00:00.013 --> 00:00:10.013 [MUSIC] 00:00:15.594 --> 00:00:20.836 One of the defining characteristics, of modern handheld systems, is that they 00:00:20.836 --> 00:00:26.170 can keep us connected and networked, without tethering us to a single location. 00:00:27.550 --> 00:00:30.640 In this lesson we'll explore the software and 00:00:30.640 --> 00:00:35.840 programing practices that we'll need to connect your applications to the network. 00:00:37.230 --> 00:00:41.370 So I'll start this lesson by discussing networking in general. 00:00:41.370 --> 00:00:46.080 That discussion will focus on connecting your applications to the internet 00:00:46.080 --> 00:00:49.960 using the hypertext transfer protocol or 00:00:49.960 --> 00:00:55.420 HTTP, Specifically by using HTTP get requests. 00:00:56.940 --> 00:01:02.040 After that I'll present several classes that Android provides to support this 00:01:02.040 --> 00:01:06.760 kind of networking, And lastly I'll discuss how your applications can 00:01:06.760 --> 00:01:12.710 process the data they receive in response to these HTTP get request. 00:01:12.710 --> 00:01:17.590 In particular I'll talk about two popular data formatting languages. 00:01:17.590 --> 00:01:21.450 One, the JavaScript Object Notation Langauge or 00:01:21.450 --> 00:01:27.150 JSON, And two, the extensible markup language or XML. 00:01:27.150 --> 00:01:31.700 And I'll talk about how you parse or make sense of 00:01:31.700 --> 00:01:36.578 these HTTP responses when they're formated in one of these languages. 00:01:36.578 --> 00:01:41.410 So early hand-held devices, gave us mobility. 00:01:41.410 --> 00:01:46.250 You could move from one place to another and still perform useful computation. 00:01:47.270 --> 00:01:52.840 However, their networking capabilities were primitive by todays standards. 00:01:52.840 --> 00:01:57.820 Now moving forward todays devices combine power processors with 00:01:57.820 --> 00:02:02.550 fast network connections over WiFi and cellular networks. 00:02:03.670 --> 00:02:08.275 Handheld applications will therefore often want to make use, 00:02:08.275 --> 00:02:14.040 are these networking capabilities to access and provide data and services. 00:02:15.710 --> 00:02:21.510 Now to help you do this, Android includes a variety of networking support classes 00:02:21.510 --> 00:02:26.889 including the socket and URL classes in the Java.net packages. 00:02:28.020 --> 00:02:36.750 The HttpRequest and HttpResponse classes in the org.appache packages, and 00:02:36.750 --> 00:02:44.630 the URI AndroidHttpClient and AudioStream classes in the android.net packages. 00:02:46.220 --> 00:02:50.620 In this lesson we're going to look at several of these classes. 00:02:50.620 --> 00:02:54.700 Using each of them to implement the same example application. 00:02:55.720 --> 00:03:01.290 This application interacts with an internet service to get information about 00:03:01.290 --> 00:03:05.470 earthquakes that have occurred in a particular geographic Region. 00:03:05.470 --> 00:03:10.930 And as you'll see, that data is returned in various formats. 00:03:10.930 --> 00:03:15.600 Now, initially we'll just display the downloaded text as is. 00:03:15.600 --> 00:03:18.660 Later on in the lesson, I'll show you how to process that 00:03:18.660 --> 00:03:23.520 data to extract just the information that you want. 00:03:23.520 --> 00:03:25.590 Oh, and and one other thing. 00:03:25.590 --> 00:03:30.940 As you'll see in a second, because this data includes geographic information, 00:03:30.940 --> 00:03:35.870 it's really begging to be displayed on a map, rather than as text. 00:03:35.870 --> 00:03:39.460 Now, we won't do that in this lesson, but keep this in 00:03:39.460 --> 00:03:44.470 mind because we'll come back to this when we get to the lesson on maps and location. 00:03:45.900 --> 00:03:52.460 So in order to make this application work, the code needs to create an http request, 00:03:52.460 --> 00:03:57.910 send it to a server computer, retrieve the results, and then display those results. 00:03:59.120 --> 00:04:02.060 Android provides several classes for helping with this. 00:04:03.450 --> 00:04:07.000 Three we'll talk about now are the socket class. 00:04:08.520 --> 00:04:14.450 The HTTPURLConnection class and the AndroidHTTPClient. 00:04:14.450 --> 00:04:23.780 I'll launch the networking socket application. 00:04:25.490 --> 00:04:26.570 As you can see, 00:04:26.570 --> 00:04:31.580 this application initially displays a single button labeled Load Data. 00:04:32.920 --> 00:04:39.040 When I press that button, the application will issue an HTTP GET request 00:04:39.040 --> 00:04:45.520 to an external server, and that server will respond with some complex text, 00:04:45.520 --> 00:04:47.890 containing the requested earth quake data. 00:04:49.400 --> 00:04:52.650 Okay, so now I'll press the Load Data button, 00:04:53.810 --> 00:04:57.320 and there you can see the requested data. 00:04:58.800 --> 00:05:03.330 Let's look at the source code to see what it took to get that data. 00:05:03.330 --> 00:05:05.500 Now, here I've opened the application in the IDE. 00:05:08.530 --> 00:05:10.330 Now, I'll open the main activity for 00:05:10.330 --> 00:05:16.550 this application, and here I'm showing the listener for the load data button. 00:05:18.940 --> 00:05:22.570 When this button is pressed, the application will create and 00:05:22.570 --> 00:05:27.970 then execute an async task called HTTP get task. 00:05:29.280 --> 00:05:30.150 Let's look at that class. 00:05:32.280 --> 00:05:37.200 The HTTP get task class first declares some variables that 00:05:37.200 --> 00:05:41.010 are used in creating an HTTP get request. 00:05:43.170 --> 00:05:48.530 When the execute method is called on the HTTP get task. 00:05:48.530 --> 00:05:50.470 The do in background method is called. 00:05:52.290 --> 00:05:57.490 That method begins by creating a new socket that will be connected to the host 00:05:57.490 --> 00:06:07.400 computer API.geoname.org on the standard http port, port 80. 00:06:07.400 --> 00:06:11.700 Next, the code gets the sockets output stream, and 00:06:11.700 --> 00:06:17.670 then writes the http get command, and this string will be sent to the host computer. 00:06:18.860 --> 00:06:22.830 Which interprets it as a HTTP GET request, and 00:06:22.830 --> 00:06:28.850 then responds by sending back the appropriate, response data, and 00:06:28.850 --> 00:06:34.030 then this code continues by getting the socket's input stream and 00:06:34.030 --> 00:06:37.030 by passing it to a method called read stream. 00:06:38.740 --> 00:06:43.830 The read stream method ultimately reads the response data from the socket's input 00:06:43.830 --> 00:06:48.570 stream, and then returns the response as a single string. 00:06:48.570 --> 00:06:55.770 And this string is then passed to the on post execute method which 00:06:55.770 --> 00:07:01.580 executes on the main thread, and which displays the response in the text view. 00:07:02.740 --> 00:07:05.230 We turn back to the application. 00:07:05.230 --> 00:07:10.490 You'll notice that the response text includes not only the earthquake data but 00:07:10.490 --> 00:07:14.020 also the HTTP response headers. 00:07:14.020 --> 00:07:18.000 Now normally I wouldn't want to display that text here. 00:07:18.000 --> 00:07:20.770 I'd really just want to show you the earthquake data. 00:07:20.770 --> 00:07:24.710 So in this case I should have parsed the response, and 00:07:24.710 --> 00:07:26.640 pulled out just the data that I wanted. 00:07:28.730 --> 00:07:31.650 In addition, you might have noticed that I didn't write any of 00:07:31.650 --> 00:07:36.490 the error handling code that you'd really need to make this application robust. 00:07:37.830 --> 00:07:42.420 And these points capture pretty well the tradeoffs of using sockets. 00:07:42.420 --> 00:07:44.030 They're very low level. 00:07:44.030 --> 00:07:47.170 You can write whatever you want on the socket, but 00:07:47.170 --> 00:07:53.720 in return, you have to handle all the many details of making the http requests, 00:07:53.720 --> 00:07:58.630 all the error handling, and all the processing of the http responses. 00:08:02.250 --> 00:08:09.140 The next implementation we'll look at uses the http URL connection class. 00:08:09.140 --> 00:08:13.060 This class provides a higher level interface that handles more of 00:08:13.060 --> 00:08:18.990 the networking details than the socket class does, but, as we'll see in a moment, 00:08:18.990 --> 00:08:24.140 it also has a less flexible API than our last option, the H, 00:08:24.140 --> 00:08:27.299 the Android HTTP client class. 00:08:28.590 --> 00:08:32.539 Now having said that, I'll also point out that the Android team is not 00:08:32.539 --> 00:08:37.120 actively working on the Android HTTP Client anymore, and 00:08:37.120 --> 00:08:42.220 it's putting its efforts into improving this class going forward. 00:08:42.220 --> 00:08:46.860 So, let's look at the example application implemented this time 00:08:46.860 --> 00:08:50.839 with the HTTP URL connection class. 00:08:53.100 --> 00:08:57.490 Now I'll launch the networking URL application. 00:08:57.490 --> 00:09:01.760 As before, this application initially displays a single button 00:09:01.760 --> 00:09:07.180 labeled load data, and as before, when I press on that button, 00:09:07.180 --> 00:09:11.740 the application will issue an HTTP get request. 00:09:11.740 --> 00:09:13.480 To an external server. 00:09:13.480 --> 00:09:17.490 And that server will respond with some complex text 00:09:17.490 --> 00:09:20.520 containing the requested earthquake data. 00:09:20.520 --> 00:09:21.180 Okay. 00:09:21.180 --> 00:09:23.280 So now I'll press the Low Data button. 00:09:24.800 --> 00:09:28.700 There you can see the requested data appearing in the text view. 00:09:29.740 --> 00:09:34.670 Notice however that this time the HTTP response headers have been stripped out. 00:09:36.630 --> 00:09:38.960 Let's look at the source code and see how this works. 00:09:41.440 --> 00:09:44.130 Now here I've got the application opened in the IDE. 00:09:45.230 --> 00:09:47.480 Now I'll open the main activity for 00:09:47.480 --> 00:09:53.140 this application, and here I'm showing the listener for the load data button. 00:09:54.650 --> 00:09:59.240 As before, when this button is pressed the application will create and 00:09:59.240 --> 00:10:02.640 then execute an asynch task called httpGetTask. 00:10:05.450 --> 00:10:06.710 Let's look at that class. 00:10:09.030 --> 00:10:12.270 When the execute method is called on HttpGetTask, 00:10:12.270 --> 00:10:15.260 the doInBackground method is invoked. 00:10:15.260 --> 00:10:20.020 That method begins by creating a new URL object and 00:10:20.020 --> 00:10:24.830 passing a URL string for the desired service as a parameter. 00:10:26.780 --> 00:10:31.510 The code then calls the open connection method on the url object. 00:10:31.510 --> 00:10:36.020 Which returns an http url connection. 00:10:36.020 --> 00:10:40.980 This object is then stored in a variable called http url connection. 00:10:40.980 --> 00:10:46.940 The code continues by getting the http url connection's 00:10:46.940 --> 00:10:51.000 input stream, and bypassing it to the read stream method. 00:10:52.650 --> 00:10:56.930 And as before, the read stream method reads the response data from 00:10:56.930 --> 00:11:02.170 the socket's input stream, and then returns the response as a single string. 00:11:03.310 --> 00:11:08.980 This time, however, the http URL connection strips off the http 00:11:08.980 --> 00:11:13.540 response headers, and handles the error checking for you. 00:11:15.140 --> 00:11:19.790 Now this string is then passed to the onPost execute method. 00:11:19.790 --> 00:11:22.460 Which displays the response in the text view. 00:11:25.120 --> 00:11:27.750 The third class is AndroidHTTPClient. 00:11:29.300 --> 00:11:35.190 This class is an implementation of the Apache Project's DefaultHttpClient. 00:11:36.380 --> 00:11:39.300 And it allows a great deal of customization. 00:11:39.300 --> 00:11:45.210 In particular, the class breaks an HTTP transaction into 00:11:45.210 --> 00:11:48.940 a request object, and into a response object. 00:11:48.940 --> 00:11:53.940 So you can create subclasses that customize the handling of requests and 00:11:53.940 --> 00:11:55.680 their responses. 00:11:55.680 --> 00:11:59.450 Now by this point you know what the application looks like so 00:11:59.450 --> 00:12:02.640 let's jump straight into the code and look at the implementation. 00:12:05.470 --> 00:12:10.370 Now here I've got the NetworkingAndroidHttpClient application. 00:12:10.370 --> 00:12:11.290 Open in the IDE. 00:12:13.050 --> 00:12:18.350 Now, I'll open the main activity for this application, and let's go right to 00:12:18.350 --> 00:12:25.290 the http get task class, that class begins by creating a new Android 00:12:25.290 --> 00:12:30.910 http client object, by calling the classes new instance method. 00:12:32.710 --> 00:12:38.480 Now, when the doInBackground method is called, the code creates an HttpGet 00:12:38.480 --> 00:12:42.650 object, passing in the URL string for that request. 00:12:43.710 --> 00:12:47.320 Next, it creates a ResponseHandler object. 00:12:47.320 --> 00:12:51.480 This object is responsible for handling the response. 00:12:51.480 --> 00:12:57.560 To the http get request, in this case the response handler is of type, 00:12:57.560 --> 00:13:03.480 Basic Response Handler, which will return the responses body. 00:13:03.480 --> 00:13:07.510 Now we'll see a more complex response handler later in this lesson. 00:13:09.290 --> 00:13:15.680 And finally the request and the response handler are passed into the execute method 00:13:15.680 --> 00:13:21.400 which sends the request, gets the response, passing it through the response 00:13:21.400 --> 00:13:29.090 handler, and the result of all this is then passed on to on post execute. 00:13:29.090 --> 00:13:31.343 Which displays the response in a text view.