[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.12,0:00:06.27,Default,,0000,0000,0000,,[MUSIC] Dialogue: 0,0:00:06.27,0:00:09.88,Default,,0000,0000,0000,,(University of Maryland) Dialogue: 0,0:00:10.85,0:00:14.62,Default,,0000,0000,0000,,(Programming Mobile Applications for\NAndroid Handheld Systems) Dialogue: 0,0:00:15.64,0:00:20.85,Default,,0000,0000,0000,,One of the defining characteristics of \Nmodern hand held systems is that they can Dialogue: 0,0:00:20.85,0:00:26.27,Default,,0000,0000,0000,,keep us connected and networked without \Ntethering us to a single location. Dialogue: 0,0:00:27.45,0:00:30.40,Default,,0000,0000,0000,,In this lesson, we'll explore \Nthe software and Dialogue: 0,0:00:30.42,0:00:36.25,Default,,0000,0000,0000,,programming practices that you'll need to \Nconnect your applications to the network. Dialogue: 0,0:00:37.09,0:00:40.71,Default,,0000,0000,0000,,So I'll start this lesson by discussing \Nnetworking in general. Dialogue: 0,0:00:41.26,0:00:46.33,Default,,0000,0000,0000,,That discussion will focus on connecting \Nyour applications to the internet using Dialogue: 0,0:00:46.33,0:00:55.59,Default,,0000,0000,0000,,the Hypertext Transfer Protocol or HTTP \Nspecifically by using HTTP GET requests. Dialogue: 0,0:00:56.83,0:00:59.82,Default,,0000,0000,0000,,After that, I'll present \Nseveral classes that Dialogue: 0,0:00:59.82,0:01:03.08,Default,,0000,0000,0000,,Android provides to support \Nthis kind of networking. Dialogue: 0,0:01:04.00,0:01:07.62,Default,,0000,0000,0000,,And lastly, I'll discuss how your \Napplications can process the Dialogue: 0,0:01:07.62,0:01:12.30,Default,,0000,0000,0000,,data they receive in response \Nto these HTTP GET requests. Dialogue: 0,0:01:12.62,0:01:17.15,Default,,0000,0000,0000,,In particular, I'll talk about two \Npopular data formatting languages. Dialogue: 0,0:01:17.36,0:01:22.36,Default,,0000,0000,0000,,One, the Javascript \NObject Notation Language, or JSON. Dialogue: 0,0:01:22.88,0:01:27.60,Default,,0000,0000,0000,,And two, the Extensible Markup Language, \Nor XML. Dialogue: 0,0:01:28.25,0:01:31.71,Default,,0000,0000,0000,,And I'll talk about how you parse, \Nor make sense, of Dialogue: 0,0:01:31.71,0:01:35.83,Default,,0000,0000,0000,,these HTTP responses when they're \Nformatted in one of these languages. Dialogue: 0,0:01:37.91,0:01:41.10,Default,,0000,0000,0000,,So early handheld devices \Ngave us mobility. Dialogue: 0,0:01:41.34,0:01:46.44,Default,,0000,0000,0000,,You could move from one place to another, \Nand still perform useful computation. Dialogue: 0,0:01:47.16,0:01:48.03,Default,,0000,0000,0000,,However, Dialogue: 0,0:01:48.03,0:01:51.82,Default,,0000,0000,0000,,their networking capabilities \Nwere primitive by today's standards. Dialogue: 0,0:01:52.76,0:01:57.38,Default,,0000,0000,0000,,Now moving forward, today's devices \Ncombine powerful processors Dialogue: 0,0:01:57.38,0:02:02.62,Default,,0000,0000,0000,,with fast network connections \Nover WiFi and cellular networks. Dialogue: 0,0:02:03.50,0:02:07.53,Default,,0000,0000,0000,,Handheld applications will therefore \Noften want to make use Dialogue: 0,0:02:08.12,0:02:13.14,Default,,0000,0000,0000,,of these networking capabilities to \Naccess and provide data, and services. \N Dialogue: 0,0:02:13.14,0:02:18.24,Default,,0000,0000,0000,,Now, to help you do this, Android includes Dialogue: 0,0:02:18.24,0:02:24.83,Default,,0000,0000,0000,,a variety of networking support classes, \Nincluding the Socket and URL classes, Dialogue: 0,0:02:24.84,0:02:26.90,Default,,0000,0000,0000,,in the Java.net packages. Dialogue: 0,0:02:27.56,0:02:35.76,Default,,0000,0000,0000,,The HttpRequest and HttpResponse classes, \Nin the org.apache packages. Dialogue: 0,0:02:36.62,0:02:42.45,Default,,0000,0000,0000,,And the URI, AndroidHttpClient,\Nand AudioStream classes, Dialogue: 0,0:02:42.74,0:02:44.100,Default,,0000,0000,0000,,in the android.net packages. Dialogue: 0,0:02:46.17,0:02:50.04,Default,,0000,0000,0000,,In this lesson we're going to look at \Nseveral of these classes, Dialogue: 0,0:02:50.40,0:02:54.96,Default,,0000,0000,0000,,using each of them to implement \Nthe same example application. Dialogue: 0,0:02:55.59,0:02:58.87,Default,,0000,0000,0000,,And this application interacts \Nwith an internet service Dialogue: 0,0:02:59.23,0:03:02.84,Default,,0000,0000,0000,,to get information about \Nearthquakes that have occurred Dialogue: 0,0:03:02.84,0:03:05.59,Default,,0000,0000,0000,,in a particular geographic region. Dialogue: 0,0:03:06.21,0:03:10.52,Default,,0000,0000,0000,,And as you'll see, that data is returned \Nin various formats. Dialogue: 0,0:03:10.87,0:03:14.81,Default,,0000,0000,0000,,Now initially we'll just display the \Ndownloaded text as is. Dialogue: 0,0:03:15.52,0:03:19.13,Default,,0000,0000,0000,,Later on in the lesson, I'll show you \Nhow to process that data Dialogue: 0,0:03:19.48,0:03:22.88,Default,,0000,0000,0000,,to extract \Njust the information that you want. Dialogue: 0,0:03:23.53,0:03:25.29,Default,,0000,0000,0000,,Oh, and, and one other thing. Dialogue: 0,0:03:25.60,0:03:26.89,Default,,0000,0000,0000,,As you'll see in a second, Dialogue: 0,0:03:27.10,0:03:30.43,Default,,0000,0000,0000,,because this data includes \Ngeographic information, Dialogue: 0,0:03:30.80,0:03:35.67,Default,,0000,0000,0000,,it's really begging to be displayed \Non a map, rather than as text. Dialogue: 0,0:03:35.67,0:03:39.74,Default,,0000,0000,0000,,Now, we won't do that in this lesson, \Nbut keep this in mind, Dialogue: 0,0:03:39.74,0:03:44.73,Default,,0000,0000,0000,,because we'll come back to this when we \Nget to the lesson on maps and location. Dialogue: 0,0:03:45.74,0:03:49.82,Default,,0000,0000,0000,,So in order to make this application \Nwork, the code needs to create Dialogue: 0,0:03:49.82,0:03:55.95,Default,,0000,0000,0000,,an HTTP request, send it to a \Nserver computer, retrieve the results, Dialogue: 0,0:03:56.34,0:04:00.99,Default,,0000,0000,0000,,and then display those results. \NAndroid provides several classes for Dialogue: 0,0:04:00.99,0:04:05.74,Default,,0000,0000,0000,,helping with this. \NThree we'll talk about now are Dialogue: 0,0:04:05.74,0:04:11.62,Default,,0000,0000,0000,,the Socket class, the HttpUrlConnection \Nclass Dialogue: 0,0:04:12.02,0:04:15.19,Default,,0000,0000,0000,,and the AndroidHttpClient. Dialogue: 0,0:04:20.49,0:04:23.91,Default,,0000,0000,0000,,I'll launch the networking \Nsocket application. Dialogue: 0,0:04:25.28,0:04:26.36,Default,,0000,0000,0000,,As you can see, Dialogue: 0,0:04:26.63,0:04:31.76,Default,,0000,0000,0000,,this application initially displays \Na single button labeled LoadData. Dialogue: 0,0:04:32.67,0:04:36.11,Default,,0000,0000,0000,,When I press that button, \Nthe application will issue Dialogue: 0,0:04:36.11,0:04:40.57,Default,,0000,0000,0000,,an HTTP GET request to an \Nexternal server. Dialogue: 0,0:04:41.08,0:04:44.98,Default,,0000,0000,0000,,And that server will respond \Nwith some complex text Dialogue: 0,0:04:45.39,0:04:48.06,Default,,0000,0000,0000,,containing the requested \Nearthquake data. Dialogue: 0,0:04:49.19,0:04:50.01,Default,,0000,0000,0000,,Okay. Dialogue: 0,0:04:50.40,0:04:57.33,Default,,0000,0000,0000,,So now I'll press the Load Data button \Nand there you can see the requested data. Dialogue: 0,0:04:58.60,0:05:02.94,Default,,0000,0000,0000,,Let's look at the source code \Nto see what it took to get that data. Dialogue: 0,0:05:03.28,0:05:05.80,Default,,0000,0000,0000,,Now here I've opened the application \Nin the IDE. Dialogue: 0,0:05:08.26,0:05:11.30,Default,,0000,0000,0000,,Now I'll open the main activity \Nfor this application. Dialogue: 0,0:05:13.38,0:05:16.77,Default,,0000,0000,0000,,And here I'm showing the listener \Nfor the Load Data button. Dialogue: 0,0:05:18.68,0:05:23.78,Default,,0000,0000,0000,,When this button is pressed, the \Napplication will create, and then execute, Dialogue: 0,0:05:23.78,0:05:30.36,Default,,0000,0000,0000,,an AsyncTask called HttpGetTask.\NLet's look at that class. Dialogue: 0,0:05:31.72,0:05:36.86,Default,,0000,0000,0000,,The HttpGetTask class first declares \Nsome variables, Dialogue: 0,0:05:37.04,0:05:41.14,Default,,0000,0000,0000,,they're used in creating \Nan HTTP GET Request. Dialogue: 0,0:05:42.92,0:05:47.73,Default,,0000,0000,0000,,When the execute method is called, on\Nthe HttpGetTask, Dialogue: 0,0:05:48.48,0:05:50.67,Default,,0000,0000,0000,,the doInBackground method is called. Dialogue: 0,0:05:51.99,0:05:54.98,Default,,0000,0000,0000,,And that method begins \Nby creating a new socket Dialogue: 0,0:05:55.61,0:06:02.00,Default,,0000,0000,0000,,that will be connected to \Nthe host computer, api.geonames.org, Dialogue: 0,0:06:02.86,0:06:08.66,Default,,0000,0000,0000,,on the standard http port, port 80.\NNext, the code gets Dialogue: 0,0:06:08.66,0:06:14.14,Default,,0000,0000,0000,,the socket's output stream, \Nand then writes the HTTP{\u1}GET{\u0}COMMAND. Dialogue: 0,0:06:15.10,0:06:19.85,Default,,0000,0000,0000,,And this string will be sent to \Nthe host computer, which interprets it Dialogue: 0,0:06:19.85,0:06:23.99,Default,,0000,0000,0000,,as an HTTPGetRequest, and then responds Dialogue: 0,0:06:23.99,0:06:27.44,Default,,0000,0000,0000,,by sending back \Nthe appropriate response data. Dialogue: 0,0:06:28.59,0:06:32.93,Default,,0000,0000,0000,,And then this code continues \Nby getting the socket's input stream Dialogue: 0,0:06:33.72,0:06:37.24,Default,,0000,0000,0000,,And by passing it to a method \Ncalled readStream. Dialogue: 0,0:06:38.63,0:06:42.51,Default,,0000,0000,0000,,The readStream method ultimately \Nreads the response data Dialogue: 0,0:06:42.76,0:06:48.85,Default,,0000,0000,0000,,from the socket's InputStream and then \Nreturns the response as a single string. Dialogue: 0,0:06:50.38,0:06:52.42,Default,,0000,0000,0000,,And this string is passed to the Dialogue: 0,0:06:52.42,0:06:57.69,Default,,0000,0000,0000,,onPostExecute method which executes \Non the main thread Dialogue: 0,0:06:58.54,0:07:01.75,Default,,0000,0000,0000,,and which displays the response \Nin the text view. Dialogue: 0,0:07:02.48,0:07:07.17,Default,,0000,0000,0000,,If we return back to the application, \Nyou'll notice that the response text Dialogue: 0,0:07:07.49,0:07:13.00,Default,,0000,0000,0000,,includes not only the earthquake data, \Nbut also the HTTP response headers. Dialogue: 0,0:07:13.90,0:07:17.55,Default,,0000,0000,0000,,Now normally, I wouldn't want \Nto display that text here. Dialogue: 0,0:07:17.94,0:07:20.36,Default,,0000,0000,0000,,I really just want to show you \Nthe earthquake data. Dialogue: 0,0:07:20.78,0:07:24.16,Default,,0000,0000,0000,,So in the case, I should have \Nparsed the response Dialogue: 0,0:07:24.52,0:07:26.86,Default,,0000,0000,0000,,and pulled out \Njust the data that I wanted. Dialogue: 0,0:07:28.53,0:07:31.66,Default,,0000,0000,0000,,In addition, you might have noticed \NI didn't write any of the Dialogue: 0,0:07:31.66,0:07:36.57,Default,,0000,0000,0000,,error handling code that you'd really \Nneed to make this application robust. Dialogue: 0,0:07:37.58,0:07:42.44,Default,,0000,0000,0000,,And these points capture pretty well \Nthe trade-offs of using sockets. Dialogue: 0,0:07:42.44,0:07:46.37,Default,,0000,0000,0000,,They're very low level: you can write \Nwhatever you want on the socket, Dialogue: 0,0:07:46.98,0:07:50.90,Default,,0000,0000,0000,,but in return, you have to handle \Nall the many details Dialogue: 0,0:07:50.91,0:07:54.86,Default,,0000,0000,0000,,of making the HTTP requests, \Nall the error handling, Dialogue: 0,0:07:55.20,0:07:58.64,Default,,0000,0000,0000,,and all the processing \Nof the HTTP responses. Dialogue: 0,0:08:02.16,0:08:08.26,Default,,0000,0000,0000,,The next implementation we'll look at \Nuses the HttpUrlConnection class. Dialogue: 0,0:08:08.90,0:08:12.80,Default,,0000,0000,0000,,This class provides a higher-level \Ninterface that handles more Dialogue: 0,0:08:12.80,0:08:17.03,Default,,0000,0000,0000,,of the networking details \Nthan the socket class does. Dialogue: 0,0:08:17.63,0:08:21.46,Default,,0000,0000,0000,,But as we'll see in a moment, \Nit also has a less flexible API Dialogue: 0,0:08:21.91,0:08:27.57,Default,,0000,0000,0000,,than our last option, \Nthe Android HTTP client class. Dialogue: 0,0:08:28.48,0:08:32.12,Default,,0000,0000,0000,,Now, having said that, I'll also \Npoint out that the Android team Dialogue: 0,0:08:32.12,0:08:36.96,Default,,0000,0000,0000,,is not actively working on the \NAndroid HTTP client anymore. Dialogue: 0,0:08:36.96,0:08:41.54,Default,,0000,0000,0000,,And is putting its efforts into \Nimproving this class going forward. Dialogue: 0,0:08:42.03,0:08:45.05,Default,,0000,0000,0000,,So let's look at the example application Dialogue: 0,0:08:45.05,0:08:50.69,Default,,0000,0000,0000,,implemented this time \Nwith the HttpURLConnection class. Dialogue: 0,0:08:51.48,0:08:52.24,Default,,0000,0000,0000,,... Dialogue: 0,0:08:52.24,0:08:53.08,Default,,0000,0000,0000,,... Dialogue: 0,0:08:53.08,0:08:56.62,Default,,0000,0000,0000,,Now I'll launch \Nthe NetworkingURL application. Dialogue: 0,0:08:57.28,0:09:00.20,Default,,0000,0000,0000,,As before, this application \Ninitially displays Dialogue: 0,0:09:00.20,0:09:02.88,Default,,0000,0000,0000,,a single button labeled Load Data. Dialogue: 0,0:09:04.12,0:09:08.50,Default,,0000,0000,0000,,And as before, when I press on \Nthat button the application will issue Dialogue: 0,0:09:08.50,0:09:15.33,Default,,0000,0000,0000,,an HTTP GET request to an external server, \Nand that server will respond Dialogue: 0,0:09:15.33,0:09:19.76,Default,,0000,0000,0000,,with some complex text, containing \Nthe requested earthquake data. Dialogue: 0,0:09:20.31,0:09:22.22,Default,,0000,0000,0000,,Okay, so now I'll press the Dialogue: 0,0:09:22.22,0:09:23.55,Default,,0000,0000,0000,,Load Data button. Dialogue: 0,0:09:24.62,0:09:28.80,Default,,0000,0000,0000,,There you can see the requested data, \Nappearing in a text view. Dialogue: 0,0:09:29.63,0:09:34.76,Default,,0000,0000,0000,,Notice, however, that this time, the HTTP \Nresponse headers have been stripped out. Dialogue: 0,0:09:36.52,0:09:39.10,Default,,0000,0000,0000,,Let's look at the source code \Nand see how this works. Dialogue: 0,0:09:41.15,0:09:44.24,Default,,0000,0000,0000,,Now, here I've got the application \Nopened in the IDE. Dialogue: 0,0:09:45.17,0:09:48.75,Default,,0000,0000,0000,,Now, I'll open the main activity \Nfor this application. Dialogue: 0,0:09:49.76,0:09:53.16,Default,,0000,0000,0000,,And here, I'm showing the listener \Nfor the load data button. Dialogue: 0,0:09:54.38,0:09:59.28,Default,,0000,0000,0000,,As before, when this button is pressed, \Nthe application will create and then Dialogue: 0,0:09:59.28,0:10:06.75,Default,,0000,0000,0000,,execute an AsyncTask called, HttpGetTask. \NLet's look at that class. Dialogue: 0,0:10:08.71,0:10:13.94,Default,,0000,0000,0000,,When the execute method is called on \NHTTPGetTask, the doInBackground method Dialogue: 0,0:10:13.94,0:10:19.12,Default,,0000,0000,0000,,is invoked. That method begins \Nby creating a new URL object, Dialogue: 0,0:10:19.72,0:10:25.09,Default,,0000,0000,0000,,and passing a URL string \Nfor the desired service as a parameter. Dialogue: 0,0:10:26.48,0:10:29.48,Default,,0000,0000,0000,,The code then calls the \Nopen connection method Dialogue: 0,0:10:29.48,0:10:35.25,Default,,0000,0000,0000,,on the URL object, which returns \Nan httpUrlConnection. Dialogue: 0,0:10:35.87,0:10:41.12,Default,,0000,0000,0000,,This object is then stored in a variable \Ncalled HttpURLConnection. Dialogue: 0,0:10:42.39,0:10:47.100,Default,,0000,0000,0000,,The code continues by getting the \NHttpURLConnection's input stream, Dialogue: 0,0:10:48.56,0:10:51.41,Default,,0000,0000,0000,,and by passing it through \Nthe readStream method. Dialogue: 0,0:10:52.44,0:10:56.73,Default,,0000,0000,0000,,And as before, the readStream method \Nreads the response data Dialogue: 0,0:10:56.73,0:11:00.84,Default,,0000,0000,0000,,from the socket's input stream, \Nand then returns the response, Dialogue: 0,0:11:01.13,0:11:06.66,Default,,0000,0000,0000,,as a single string. \NThis time however, the HTTP URL connection Dialogue: 0,0:11:07.04,0:11:13.78,Default,,0000,0000,0000,,strips off the HTTP response headers \Nand handles the error checking for you. Dialogue: 0,0:11:14.89,0:11:18.81,Default,,0000,0000,0000,,Now this string is then passed \Nto the onPostExecute method Dialogue: 0,0:11:19.67,0:11:22.80,Default,,0000,0000,0000,,which displays the response \Nin a text view. Dialogue: 0,0:11:24.77,0:11:31.67,Default,,0000,0000,0000,,The third class is Android HTTP client.\NThis class is an implementation of the Dialogue: 0,0:11:31.67,0:11:38.78,Default,,0000,0000,0000,,Apache project's DefaultHttpClient and \Nit allows a great deal of customization. Dialogue: 0,0:11:39.18,0:11:44.39,Default,,0000,0000,0000,,In particular, the class breaks \Nan HTTP transaction into Dialogue: 0,0:11:44.98,0:11:50.61,Default,,0000,0000,0000,,a request object and into a response object. \NSo you can create subclasses Dialogue: 0,0:11:50.61,0:11:55.09,Default,,0000,0000,0000,,that customize the handling of requests \Nand their responses. Dialogue: 0,0:11:55.52,0:11:58.70,Default,,0000,0000,0000,,Now, by this point, you know \Nwhat the application looks like, Dialogue: 0,0:11:58.93,0:12:02.71,Default,,0000,0000,0000,,so let's jump straight into the code \Nand look at the implementation. Dialogue: 0,0:12:05.05,0:12:11.61,Default,,0000,0000,0000,,Now, here I've got the networking Android \NHTTP client application opened in the IDE. Dialogue: 0,0:12:12.75,0:12:16.44,Default,,0000,0000,0000,,Now, I'll open the main activity \Nfor this application. Dialogue: 0,0:12:17.04,0:12:20.79,Default,,0000,0000,0000,,And let's go right to the HTTP get task class. Dialogue: 0,0:12:22.16,0:12:24.42,Default,,0000,0000,0000,,That class begins by creating a new Dialogue: 0,0:12:24.42,0:12:31.19,Default,,0000,0000,0000,,AndroidHttpClient object by calling \Nthe classes newInstance method. Dialogue: 0,0:12:32.34,0:12:36.35,Default,,0000,0000,0000,,Now, when the doInBackground method \Nis called, the code creates Dialogue: 0,0:12:36.35,0:12:42.78,Default,,0000,0000,0000,,an HttpGet object, passing in \Nthe URL string for that request. Dialogue: 0,0:12:43.48,0:12:46.69,Default,,0000,0000,0000,,Next, it creates a \NResponseHandler object. Dialogue: 0,0:12:47.10,0:12:53.90,Default,,0000,0000,0000,,This object is responsible for handling \Nthe response to the HttpGet request. Dialogue: 0,0:12:53.90,0:12:57.51,Default,,0000,0000,0000,,In this case, the ResponseHandler \Nis of type Dialogue: 0,0:12:57.51,0:13:02.45,Default,,0000,0000,0000,,basicResponseHandler, \Nwhich will return the response's body. Dialogue: 0,0:13:03.29,0:13:07.58,Default,,0000,0000,0000,,Now we'll see a more complex \NResponseHandler later in this lesson. Dialogue: 0,0:13:08.92,0:13:12.39,Default,,0000,0000,0000,,And finally, the request and the ResponseHandler Dialogue: 0,0:13:12.67,0:13:15.07,Default,,0000,0000,0000,,are passed into the execute method, Dialogue: 0,0:13:15.46,0:13:18.93,Default,,0000,0000,0000,,which sends the request, \Ngets the response, Dialogue: 0,0:13:19.82,0:13:21.97,Default,,0000,0000,0000,,passing it through the ResponseHandler. Dialogue: 0,0:13:23.01,0:13:24.70,Default,,0000,0000,0000,,And the result of all this Dialogue: 0,0:13:24.70,0:13:28.57,Default,,0000,0000,0000,,is then passed on to onPostExecute, Dialogue: 0,0:13:28.97,0:13:31.73,Default,,0000,0000,0000,,which displays the response \Nin a text field. Dialogue: 0,0:13:31.73,0:13:36.25,Default,,0000,0000,0000,,[BLANK_AUDIO]