1 00:00:00,013 --> 00:00:10,013 [MUSIC] 2 00:00:15,594 --> 00:00:20,836 One of the defining characteristics, of modern handheld systems, is that they 3 00:00:20,836 --> 00:00:26,170 can keep us connected and networked, without tethering us to a single location. 4 00:00:27,550 --> 00:00:30,640 In this lesson we'll explore the software and 5 00:00:30,640 --> 00:00:35,840 programing practices that we'll need to connect your applications to the network. 6 00:00:37,230 --> 00:00:41,370 So I'll start this lesson by discussing networking in general. 7 00:00:41,370 --> 00:00:46,080 That discussion will focus on connecting your applications to the internet 8 00:00:46,080 --> 00:00:49,960 using the hypertext transfer protocol or 9 00:00:49,960 --> 00:00:55,420 HTTP, Specifically by using HTTP get requests. 10 00:00:56,940 --> 00:01:02,040 After that I'll present several classes that Android provides to support this 11 00:01:02,040 --> 00:01:06,760 kind of networking, And lastly I'll discuss how your applications can 12 00:01:06,760 --> 00:01:12,710 process the data they receive in response to these HTTP get request. 13 00:01:12,710 --> 00:01:17,590 In particular I'll talk about two popular data formatting languages. 14 00:01:17,590 --> 00:01:21,450 One, the JavaScript Object Notation Langauge or 15 00:01:21,450 --> 00:01:27,150 JSON, And two, the extensible markup language or XML. 16 00:01:27,150 --> 00:01:31,700 And I'll talk about how you parse or make sense of 17 00:01:31,700 --> 00:01:36,578 these HTTP responses when they're formated in one of these languages. 18 00:01:36,578 --> 00:01:41,410 So early hand-held devices, gave us mobility. 19 00:01:41,410 --> 00:01:46,250 You could move from one place to another and still perform useful computation. 20 00:01:47,270 --> 00:01:52,840 However, their networking capabilities were primitive by todays standards. 21 00:01:52,840 --> 00:01:57,820 Now moving forward todays devices combine power processors with 22 00:01:57,820 --> 00:02:02,550 fast network connections over WiFi and cellular networks. 23 00:02:03,670 --> 00:02:08,275 Handheld applications will therefore often want to make use, 24 00:02:08,275 --> 00:02:14,040 are these networking capabilities to access and provide data and services. 25 00:02:15,710 --> 00:02:21,510 Now to help you do this, Android includes a variety of networking support classes 26 00:02:21,510 --> 00:02:26,889 including the socket and URL classes in the Java.net packages. 27 00:02:28,020 --> 00:02:36,750 The HttpRequest and HttpResponse classes in the org.appache packages, and 28 00:02:36,750 --> 00:02:44,630 the URI AndroidHttpClient and AudioStream classes in the android.net packages. 29 00:02:46,220 --> 00:02:50,620 In this lesson we're going to look at several of these classes. 30 00:02:50,620 --> 00:02:54,700 Using each of them to implement the same example application. 31 00:02:55,720 --> 00:03:01,290 This application interacts with an internet service to get information about 32 00:03:01,290 --> 00:03:05,470 earthquakes that have occurred in a particular geographic Region. 33 00:03:05,470 --> 00:03:10,930 And as you'll see, that data is returned in various formats. 34 00:03:10,930 --> 00:03:15,600 Now, initially we'll just display the downloaded text as is. 35 00:03:15,600 --> 00:03:18,660 Later on in the lesson, I'll show you how to process that 36 00:03:18,660 --> 00:03:23,520 data to extract just the information that you want. 37 00:03:23,520 --> 00:03:25,590 Oh, and and one other thing. 38 00:03:25,590 --> 00:03:30,940 As you'll see in a second, because this data includes geographic information, 39 00:03:30,940 --> 00:03:35,870 it's really begging to be displayed on a map, rather than as text. 40 00:03:35,870 --> 00:03:39,460 Now, we won't do that in this lesson, but keep this in 41 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. 42 00:03:45,900 --> 00:03:52,460 So in order to make this application work, the code needs to create an http request, 43 00:03:52,460 --> 00:03:57,910 send it to a server computer, retrieve the results, and then display those results. 44 00:03:59,120 --> 00:04:02,060 Android provides several classes for helping with this. 45 00:04:03,450 --> 00:04:07,000 Three we'll talk about now are the socket class. 46 00:04:08,520 --> 00:04:14,450 The HTTPURLConnection class and the AndroidHTTPClient. 47 00:04:14,450 --> 00:04:23,780 I'll launch the networking socket application. 48 00:04:25,490 --> 00:04:26,570 As you can see, 49 00:04:26,570 --> 00:04:31,580 this application initially displays a single button labeled Load Data. 50 00:04:32,920 --> 00:04:39,040 When I press that button, the application will issue an HTTP GET request 51 00:04:39,040 --> 00:04:45,520 to an external server, and that server will respond with some complex text, 52 00:04:45,520 --> 00:04:47,890 containing the requested earth quake data. 53 00:04:49,400 --> 00:04:52,650 Okay, so now I'll press the Load Data button, 54 00:04:53,810 --> 00:04:57,320 and there you can see the requested data. 55 00:04:58,800 --> 00:05:03,330 Let's look at the source code to see what it took to get that data. 56 00:05:03,330 --> 00:05:05,500 Now, here I've opened the application in the IDE. 57 00:05:08,530 --> 00:05:10,330 Now, I'll open the main activity for 58 00:05:10,330 --> 00:05:16,550 this application, and here I'm showing the listener for the load data button. 59 00:05:18,940 --> 00:05:22,570 When this button is pressed, the application will create and 60 00:05:22,570 --> 00:05:27,970 then execute an async task called HTTP get task. 61 00:05:29,280 --> 00:05:30,150 Let's look at that class. 62 00:05:32,280 --> 00:05:37,200 The HTTP get task class first declares some variables that 63 00:05:37,200 --> 00:05:41,010 are used in creating an HTTP get request. 64 00:05:43,170 --> 00:05:48,530 When the execute method is called on the HTTP get task. 65 00:05:48,530 --> 00:05:50,470 The do in background method is called. 66 00:05:52,290 --> 00:05:57,490 That method begins by creating a new socket that will be connected to the host 67 00:05:57,490 --> 00:06:07,400 computer API.geoname.org on the standard http port, port 80. 68 00:06:07,400 --> 00:06:11,700 Next, the code gets the sockets output stream, and 69 00:06:11,700 --> 00:06:17,670 then writes the http get command, and this string will be sent to the host computer. 70 00:06:18,860 --> 00:06:22,830 Which interprets it as a HTTP GET request, and 71 00:06:22,830 --> 00:06:28,850 then responds by sending back the appropriate, response data, and 72 00:06:28,850 --> 00:06:34,030 then this code continues by getting the socket's input stream and 73 00:06:34,030 --> 00:06:37,030 by passing it to a method called read stream. 74 00:06:38,740 --> 00:06:43,830 The read stream method ultimately reads the response data from the socket's input 75 00:06:43,830 --> 00:06:48,570 stream, and then returns the response as a single string. 76 00:06:48,570 --> 00:06:55,770 And this string is then passed to the on post execute method which 77 00:06:55,770 --> 00:07:01,580 executes on the main thread, and which displays the response in the text view. 78 00:07:02,740 --> 00:07:05,230 We turn back to the application. 79 00:07:05,230 --> 00:07:10,490 You'll notice that the response text includes not only the earthquake data but 80 00:07:10,490 --> 00:07:14,020 also the HTTP response headers. 81 00:07:14,020 --> 00:07:18,000 Now normally I wouldn't want to display that text here. 82 00:07:18,000 --> 00:07:20,770 I'd really just want to show you the earthquake data. 83 00:07:20,770 --> 00:07:24,710 So in this case I should have parsed the response, and 84 00:07:24,710 --> 00:07:26,640 pulled out just the data that I wanted. 85 00:07:28,730 --> 00:07:31,650 In addition, you might have noticed that I didn't write any of 86 00:07:31,650 --> 00:07:36,490 the error handling code that you'd really need to make this application robust. 87 00:07:37,830 --> 00:07:42,420 And these points capture pretty well the tradeoffs of using sockets. 88 00:07:42,420 --> 00:07:44,030 They're very low level. 89 00:07:44,030 --> 00:07:47,170 You can write whatever you want on the socket, but 90 00:07:47,170 --> 00:07:53,720 in return, you have to handle all the many details of making the http requests, 91 00:07:53,720 --> 00:07:58,630 all the error handling, and all the processing of the http responses. 92 00:08:02,250 --> 00:08:09,140 The next implementation we'll look at uses the http URL connection class. 93 00:08:09,140 --> 00:08:13,060 This class provides a higher level interface that handles more of 94 00:08:13,060 --> 00:08:18,990 the networking details than the socket class does, but, as we'll see in a moment, 95 00:08:18,990 --> 00:08:24,140 it also has a less flexible API than our last option, the H, 96 00:08:24,140 --> 00:08:27,299 the Android HTTP client class. 97 00:08:28,590 --> 00:08:32,539 Now having said that, I'll also point out that the Android team is not 98 00:08:32,539 --> 00:08:37,120 actively working on the Android HTTP Client anymore, and 99 00:08:37,120 --> 00:08:42,220 it's putting its efforts into improving this class going forward. 100 00:08:42,220 --> 00:08:46,860 So, let's look at the example application implemented this time 101 00:08:46,860 --> 00:08:50,839 with the HTTP URL connection class. 102 00:08:53,100 --> 00:08:57,490 Now I'll launch the networking URL application. 103 00:08:57,490 --> 00:09:01,760 As before, this application initially displays a single button 104 00:09:01,760 --> 00:09:07,180 labeled load data, and as before, when I press on that button, 105 00:09:07,180 --> 00:09:11,740 the application will issue an HTTP get request. 106 00:09:11,740 --> 00:09:13,480 To an external server. 107 00:09:13,480 --> 00:09:17,490 And that server will respond with some complex text 108 00:09:17,490 --> 00:09:20,520 containing the requested earthquake data. 109 00:09:20,520 --> 00:09:21,180 Okay. 110 00:09:21,180 --> 00:09:23,280 So now I'll press the Low Data button. 111 00:09:24,800 --> 00:09:28,700 There you can see the requested data appearing in the text view. 112 00:09:29,740 --> 00:09:34,670 Notice however that this time the HTTP response headers have been stripped out. 113 00:09:36,630 --> 00:09:38,960 Let's look at the source code and see how this works. 114 00:09:41,440 --> 00:09:44,130 Now here I've got the application opened in the IDE. 115 00:09:45,230 --> 00:09:47,480 Now I'll open the main activity for 116 00:09:47,480 --> 00:09:53,140 this application, and here I'm showing the listener for the load data button. 117 00:09:54,650 --> 00:09:59,240 As before, when this button is pressed the application will create and 118 00:09:59,240 --> 00:10:02,640 then execute an asynch task called httpGetTask. 119 00:10:05,450 --> 00:10:06,710 Let's look at that class. 120 00:10:09,030 --> 00:10:12,270 When the execute method is called on HttpGetTask, 121 00:10:12,270 --> 00:10:15,260 the doInBackground method is invoked. 122 00:10:15,260 --> 00:10:20,020 That method begins by creating a new URL object and 123 00:10:20,020 --> 00:10:24,830 passing a URL string for the desired service as a parameter. 124 00:10:26,780 --> 00:10:31,510 The code then calls the open connection method on the url object. 125 00:10:31,510 --> 00:10:36,020 Which returns an http url connection. 126 00:10:36,020 --> 00:10:40,980 This object is then stored in a variable called http url connection. 127 00:10:40,980 --> 00:10:46,940 The code continues by getting the http url connection's 128 00:10:46,940 --> 00:10:51,000 input stream, and bypassing it to the read stream method. 129 00:10:52,650 --> 00:10:56,930 And as before, the read stream method reads the response data from 130 00:10:56,930 --> 00:11:02,170 the socket's input stream, and then returns the response as a single string. 131 00:11:03,310 --> 00:11:08,980 This time, however, the http URL connection strips off the http 132 00:11:08,980 --> 00:11:13,540 response headers, and handles the error checking for you. 133 00:11:15,140 --> 00:11:19,790 Now this string is then passed to the onPost execute method. 134 00:11:19,790 --> 00:11:22,460 Which displays the response in the text view. 135 00:11:25,120 --> 00:11:27,750 The third class is AndroidHTTPClient. 136 00:11:29,300 --> 00:11:35,190 This class is an implementation of the Apache Project's DefaultHttpClient. 137 00:11:36,380 --> 00:11:39,300 And it allows a great deal of customization. 138 00:11:39,300 --> 00:11:45,210 In particular, the class breaks an HTTP transaction into 139 00:11:45,210 --> 00:11:48,940 a request object, and into a response object. 140 00:11:48,940 --> 00:11:53,940 So you can create subclasses that customize the handling of requests and 141 00:11:53,940 --> 00:11:55,680 their responses. 142 00:11:55,680 --> 00:11:59,450 Now by this point you know what the application looks like so 143 00:11:59,450 --> 00:12:02,640 let's jump straight into the code and look at the implementation. 144 00:12:05,470 --> 00:12:10,370 Now here I've got the NetworkingAndroidHttpClient application. 145 00:12:10,370 --> 00:12:11,290 Open in the IDE. 146 00:12:13,050 --> 00:12:18,350 Now, I'll open the main activity for this application, and let's go right to 147 00:12:18,350 --> 00:12:25,290 the http get task class, that class begins by creating a new Android 148 00:12:25,290 --> 00:12:30,910 http client object, by calling the classes new instance method. 149 00:12:32,710 --> 00:12:38,480 Now, when the doInBackground method is called, the code creates an HttpGet 150 00:12:38,480 --> 00:12:42,650 object, passing in the URL string for that request. 151 00:12:43,710 --> 00:12:47,320 Next, it creates a ResponseHandler object. 152 00:12:47,320 --> 00:12:51,480 This object is responsible for handling the response. 153 00:12:51,480 --> 00:12:57,560 To the http get request, in this case the response handler is of type, 154 00:12:57,560 --> 00:13:03,480 Basic Response Handler, which will return the responses body. 155 00:13:03,480 --> 00:13:07,510 Now we'll see a more complex response handler later in this lesson. 156 00:13:09,290 --> 00:13:15,680 And finally the request and the response handler are passed into the execute method 157 00:13:15,680 --> 00:13:21,400 which sends the request, gets the response, passing it through the response 158 00:13:21,400 --> 00:13:29,090 handler, and the result of all this is then passed on to on post execute. 159 00:13:29,090 --> 00:13:31,343 Which displays the response in a text view.