[MUSIC] One of the defining characteristics, of modern handheld systems, is that they can keep us connected and networked, without tethering us to a single location. In this lesson we'll explore the software and programing practices that we'll need to connect your applications to the network. So I'll start this lesson by discussing networking in general. That discussion will focus on connecting your applications to the internet using the hypertext transfer protocol or HTTP, Specifically by using HTTP get requests. After that I'll present several classes that Android provides to support this kind of networking, And lastly I'll discuss how your applications can process the data they receive in response to these HTTP get request. In particular I'll talk about two popular data formatting languages. One, the JavaScript Object Notation Langauge or JSON, And two, the extensible markup language or XML. And I'll talk about how you parse or make sense of these HTTP responses when they're formated in one of these languages. So early hand-held devices, gave us mobility. You could move from one place to another and still perform useful computation. However, their networking capabilities were primitive by todays standards. Now moving forward todays devices combine power processors with fast network connections over WiFi and cellular networks. Handheld applications will therefore often want to make use, are these networking capabilities to access and provide data and services. Now to help you do this, Android includes a variety of networking support classes including the socket and URL classes in the Java.net packages. The HttpRequest and HttpResponse classes in the org.appache packages, and the URI AndroidHttpClient and AudioStream classes in the android.net packages. In this lesson we're going to look at several of these classes. Using each of them to implement the same example application. This application interacts with an internet service to get information about earthquakes that have occurred in a particular geographic Region. And as you'll see, that data is returned in various formats. Now, initially we'll just display the downloaded text as is. Later on in the lesson, I'll show you how to process that data to extract just the information that you want. Oh, and and one other thing. As you'll see in a second, because this data includes geographic information, it's really begging to be displayed on a map, rather than as text. Now, we won't do that in this lesson, but keep this in mind because we'll come back to this when we get to the lesson on maps and location. So in order to make this application work, the code needs to create an http request, send it to a server computer, retrieve the results, and then display those results. Android provides several classes for helping with this. Three we'll talk about now are the socket class. The HTTPURLConnection class and the AndroidHTTPClient. I'll launch the networking socket application. As you can see, this application initially displays a single button labeled Load Data. When I press that button, the application will issue an HTTP GET request to an external server, and that server will respond with some complex text, containing the requested earth quake data. Okay, so now I'll press the Load Data button, and there you can see the requested data. Let's look at the source code to see what it took to get that data. Now, here I've opened the application in the IDE. Now, I'll open the main activity for this application, and here I'm showing the listener for the load data button. When this button is pressed, the application will create and then execute an async task called HTTP get task. Let's look at that class. The HTTP get task class first declares some variables that are used in creating an HTTP get request. When the execute method is called on the HTTP get task. The do in background method is called. That method begins by creating a new socket that will be connected to the host computer API.geoname.org on the standard http port, port 80. Next, the code gets the sockets output stream, and then writes the http get command, and this string will be sent to the host computer. Which interprets it as a HTTP GET request, and then responds by sending back the appropriate, response data, and then this code continues by getting the socket's input stream and by passing it to a method called read stream. The read stream method ultimately reads the response data from the socket's input stream, and then returns the response as a single string. And this string is then passed to the on post execute method which executes on the main thread, and which displays the response in the text view. We turn back to the application. You'll notice that the response text includes not only the earthquake data but also the HTTP response headers. Now normally I wouldn't want to display that text here. I'd really just want to show you the earthquake data. So in this case I should have parsed the response, and pulled out just the data that I wanted. In addition, you might have noticed that I didn't write any of the error handling code that you'd really need to make this application robust. And these points capture pretty well the tradeoffs of using sockets. They're very low level. You can write whatever you want on the socket, but in return, you have to handle all the many details of making the http requests, all the error handling, and all the processing of the http responses. The next implementation we'll look at uses the http URL connection class. This class provides a higher level interface that handles more of the networking details than the socket class does, but, as we'll see in a moment, it also has a less flexible API than our last option, the H, the Android HTTP client class. Now having said that, I'll also point out that the Android team is not actively working on the Android HTTP Client anymore, and it's putting its efforts into improving this class going forward. So, let's look at the example application implemented this time with the HTTP URL connection class. Now I'll launch the networking URL application. As before, this application initially displays a single button labeled load data, and as before, when I press on that button, the application will issue an HTTP get request. To an external server. And that server will respond with some complex text containing the requested earthquake data. Okay. So now I'll press the Low Data button. There you can see the requested data appearing in the text view. Notice however that this time the HTTP response headers have been stripped out. Let's look at the source code and see how this works. Now here I've got the application opened in the IDE. Now I'll open the main activity for this application, and here I'm showing the listener for the load data button. As before, when this button is pressed the application will create and then execute an asynch task called httpGetTask. Let's look at that class. When the execute method is called on HttpGetTask, the doInBackground method is invoked. That method begins by creating a new URL object and passing a URL string for the desired service as a parameter. The code then calls the open connection method on the url object. Which returns an http url connection. This object is then stored in a variable called http url connection. The code continues by getting the http url connection's input stream, and bypassing it to the read stream method. And as before, the read stream method reads the response data from the socket's input stream, and then returns the response as a single string. This time, however, the http URL connection strips off the http response headers, and handles the error checking for you. Now this string is then passed to the onPost execute method. Which displays the response in the text view. The third class is AndroidHTTPClient. This class is an implementation of the Apache Project's DefaultHttpClient. And it allows a great deal of customization. In particular, the class breaks an HTTP transaction into a request object, and into a response object. So you can create subclasses that customize the handling of requests and their responses. Now by this point you know what the application looks like so let's jump straight into the code and look at the implementation. Now here I've got the NetworkingAndroidHttpClient application. Open in the IDE. Now, I'll open the main activity for this application, and let's go right to the http get task class, that class begins by creating a new Android http client object, by calling the classes new instance method. Now, when the doInBackground method is called, the code creates an HttpGet object, passing in the URL string for that request. Next, it creates a ResponseHandler object. This object is responsible for handling the response. To the http get request, in this case the response handler is of type, Basic Response Handler, which will return the responses body. Now we'll see a more complex response handler later in this lesson. And finally the request and the response handler are passed into the execute method which sends the request, gets the response, passing it through the response handler, and the result of all this is then passed on to on post execute. Which displays the response in a text view.