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