Return to Video

https:/.../index.mp4

  • 0:00 - 0:06
    [MUSIC]
  • 0:06 - 0:10
    (University of Maryland)
  • 0:11 - 0:15
    (Programming Mobile Applications for
    Android Handheld Systems)
  • 0:16 - 0:21
    One of the defining characteristics of
    modern hand held systems is that they can
  • 0:21 - 0:26
    keep us connected and networked without
    tethering us to a single location.
  • 0:27 - 0:30
    In this lesson, we'll explore
    the software and
  • 0:30 - 0:36
    programming practices that you'll need to
    connect your applications to the network.
  • 0:37 - 0:41
    So I'll start this lesson by discussing
    networking in general.
  • 0:41 - 0:46
    That discussion will focus on connecting
    your applications to the internet using
  • 0:46 - 0:56
    the Hypertext Transfer Protocol or HTTP
    specifically by using HTTP GET requests.
  • 0:57 - 1:00
    After that, I'll present
    several classes that
  • 1:00 - 1:03
    Android provides to support
    this kind of networking.
  • 1:04 - 1:08
    And lastly, I'll discuss how your
    applications can process the
  • 1:08 - 1:12
    data they receive in response
    to these HTTP GET requests.
  • 1:13 - 1:17
    In particular, I'll talk about two
    popular data formatting languages.
  • 1:17 - 1:22
    One, the Javascript
    Object Notation Language, or JSON.
  • 1:23 - 1:28
    And two, the Extensible Markup Language,
    or XML.
  • 1:28 - 1:32
    And I'll talk about how you parse,
    or make sense, of
  • 1:32 - 1:36
    these HTTP responses when they're
    formatted in one of these languages.
  • 1:38 - 1:41
    So early handheld devices
    gave us mobility.
  • 1:41 - 1:46
    You could move from one place to another,
    and still perform useful computation.
  • 1:47 - 1:48
    However,
  • 1:48 - 1:52
    their networking capabilities
    were primitive by today's standards.
  • 1:53 - 1:57
    Now moving forward, today's devices
    combine powerful processors
  • 1:57 - 2:03
    with fast network connections
    over WiFi and cellular networks.
  • 2:03 - 2:08
    Handheld applications will therefore
    often want to make use
  • 2:08 - 2:13
    of these networking capabilities to
    access and provide data, and services.
  • 2:13 - 2:18
    Now, to help you do this, Android includes
  • 2:18 - 2:25
    a variety of networking support classes,
    including the Socket and URL classes,
  • 2:25 - 2:27
    in the Java.net packages.
  • 2:28 - 2:36
    The HttpRequest and HttpResponse classes,
    in the org.apache packages.
  • 2:37 - 2:42
    And the URI, AndroidHttpClient,
    and AudioStream classes,
  • 2:43 - 2:45
    in the android.net packages.
  • 2:46 - 2:50
    In this lesson we're going to look at
    several of these classes,
  • 2:50 - 2:55
    using each of them to implement
    the same example application.
  • 2:56 - 2:59
    And this application interacts
    with an internet service
  • 2:59 - 3:03
    to get information about
    earthquakes that have occurred
  • 3:03 - 3:06
    in a particular geographic region.
  • 3:06 - 3:11
    And as you'll see, that data is returned
    in various formats.
  • 3:11 - 3:15
    Now initially we'll just display the
    downloaded text as is.
  • 3:16 - 3:19
    Later on in the lesson, I'll show you
    how to process that data
  • 3:19 - 3:23
    to extract
    just the information that you want.
  • 3:24 - 3:25
    Oh, and, and one other thing.
  • 3:26 - 3:27
    As you'll see in a second,
  • 3:27 - 3:30
    because this data includes
    geographic information,
  • 3:31 - 3:36
    it's really begging to be displayed
    on a map, rather than as text.
  • 3:36 - 3:40
    Now, we won't do that in this lesson,
    but keep this in mind,
  • 3:40 - 3:45
    because we'll come back to this when we
    get to the lesson on maps and location.
  • 3:46 - 3:50
    So in order to make this application
    work, the code needs to create
  • 3:50 - 3:56
    an HTTP request, send it to a
    server computer, retrieve the results,
  • 3:56 - 4:01
    and then display those results.
    Android provides several classes for
  • 4:01 - 4:06
    helping with this.
    Three we'll talk about now are
  • 4:06 - 4:12
    the Socket class, the HttpUrlConnection
    class
  • 4:12 - 4:15
    and the AndroidHttpClient.
  • 4:20 - 4:24
    I'll launch the networking
    socket application.
  • 4:25 - 4:26
    As you can see,
  • 4:27 - 4:32
    this application initially displays
    a single button labeled LoadData.
  • 4:33 - 4:36
    When I press that button,
    the application will issue
  • 4:36 - 4:41
    an HTTP GET request to an
    external server.
  • 4:41 - 4:45
    And that server will respond
    with some complex text
  • 4:45 - 4:48
    containing the requested
    earthquake data.
  • 4:49 - 4:50
    Okay.
  • 4:50 - 4:57
    So now I'll press the Load Data button
    and there you can see the requested data.
  • 4:59 - 5:03
    Let's look at the source code
    to see what it took to get that data.
  • 5:03 - 5:06
    Now here I've opened the application
    in the IDE.
  • 5:08 - 5:11
    Now I'll open the main activity
    for this application.
  • 5:13 - 5:17
    And here I'm showing the listener
    for the Load Data button.
  • 5:19 - 5:24
    When this button is pressed, the
    application will create, and then execute,
  • 5:24 - 5:30
    an AsyncTask called HttpGetTask.
    Let's look at that class.
  • 5:32 - 5:37
    The HttpGetTask class first declares
    some variables,
  • 5:37 - 5:41
    they're used in creating
    an HTTP GET Request.
  • 5:43 - 5:48
    When the execute method is called, on
    the HttpGetTask,
  • 5:48 - 5:51
    the doInBackground method is called.
  • 5:52 - 5:55
    And that method begins
    by creating a new socket
  • 5:56 - 6:02
    that will be connected to
    the host computer, api.geonames.org,
  • 6:03 - 6:09
    on the standard http port, port 80.
    Next, the code gets
  • 6:09 - 6:14
    the socket's output stream,
    and then writes the HTTPGETCOMMAND.
  • 6:15 - 6:20
    And this string will be sent to
    the host computer, which interprets it
  • 6:20 - 6:24
    as an HTTPGetRequest, and then responds
  • 6:24 - 6:27
    by sending back
    the appropriate response data.
  • 6:29 - 6:33
    And then this code continues
    by getting the socket's input stream
  • 6:34 - 6:37
    And by passing it to a method
    called readStream.
  • 6:39 - 6:43
    The readStream method ultimately
    reads the response data
  • 6:43 - 6:49
    from the socket's InputStream and then
    returns the response as a single string.
  • 6:50 - 6:52
    And this string is passed to the
  • 6:52 - 6:58
    onPostExecute method which executes
    on the main thread
  • 6:59 - 7:02
    and which displays the response
    in the text view.
  • 7:02 - 7:07
    If we return back to the application,
    you'll notice that the response text
  • 7:07 - 7:13
    includes not only the earthquake data,
    but also the HTTP response headers.
  • 7:14 - 7:18
    Now normally, I wouldn't want
    to display that text here.
  • 7:18 - 7:20
    I really just want to show you
    the earthquake data.
  • 7:21 - 7:24
    So in the case, I should have
    parsed the response
  • 7:25 - 7:27
    and pulled out
    just the data that I wanted.
  • 7:29 - 7:32
    In addition, you might have noticed
    I didn't write any of the
  • 7:32 - 7:37
    error handling code that you'd really
    need to make this application robust.
  • 7:38 - 7:42
    And these points capture pretty well
    the trade-offs of using sockets.
  • 7:42 - 7:46
    They're very low level: you can write
    whatever you want on the socket,
  • 7:47 - 7:51
    but in return, you have to handle
    all the many details
  • 7:51 - 7:55
    of making the HTTP requests,
    all the error handling,
  • 7:55 - 7:59
    and all the processing
    of the HTTP responses.
  • 8:02 - 8:08
    The next implementation we'll look at
    uses the HttpUrlConnection class.
  • 8:09 - 8:13
    This class provides a higher-level
    interface that handles more
  • 8:13 - 8:17
    of the networking details
    than the socket class does.
  • 8:18 - 8:21
    But as we'll see in a moment,
    it also has a less flexible API
  • 8:22 - 8:28
    than our last option,
    the Android HTTP client class.
  • 8:28 - 8:32
    Now, having said that, I'll also
    point out that the Android team
  • 8:32 - 8:37
    is not actively working on the
    Android HTTP client anymore.
  • 8:37 - 8:42
    And is putting its efforts into
    improving this class going forward.
  • 8:42 - 8:45
    So let's look at the example application
  • 8:45 - 8:51
    implemented this time
    with the HttpURLConnection class.
  • 8:51 - 8:52
    ...
  • 8:52 - 8:53
    ...
  • 8:53 - 8:57
    Now I'll launch
    the NetworkingURL application.
  • 8:57 - 9:00
    As before, this application
    initially displays
  • 9:00 - 9:03
    a single button labeled Load Data.
  • 9:04 - 9:08
    And as before, when I press on
    that button the application will issue
  • 9:08 - 9:15
    an HTTP GET request to an external server,
    and that server will respond
  • 9:15 - 9:20
    with some complex text, containing
    the requested earthquake data.
  • 9:20 - 9:22
    Okay, so now I'll press the
  • 9:22 - 9:24
    Load Data button.
  • 9:25 - 9:29
    There you can see the requested data,
    appearing in a text view.
  • 9:30 - 9:35
    Notice, however, that this time, the HTTP
    response headers have been stripped out.
  • 9:37 - 9:39
    Let's look at the source code
    and see how this works.
  • 9:41 - 9:44
    Now, here I've got the application
    opened in the IDE.
  • 9:45 - 9:49
    Now, I'll open the main activity
    for this application.
  • 9:50 - 9:53
    And here, I'm showing the listener
    for the load data button.
  • 9:54 - 9:59
    As before, when this button is pressed,
    the application will create and then
  • 9:59 - 10:07
    execute an AsyncTask called, HttpGetTask.
    Let's look at that class.
  • 10:09 - 10:14
    When the execute method is called on
    HTTPGetTask, the doInBackground method
  • 10:14 - 10:19
    is invoked. That method begins
    by creating a new URL object,
  • 10:20 - 10:25
    and passing a URL string
    for the desired service as a parameter.
  • 10:26 - 10:29
    The code then calls the
    open connection method
  • 10:29 - 10:35
    on the URL object, which returns
    an httpUrlConnection.
  • 10:36 - 10:41
    This object is then stored in a variable
    called HttpURLConnection.
  • 10:42 - 10:48
    The code continues by getting the
    HttpURLConnection's input stream,
  • 10:49 - 10:51
    and by passing it through
    the readStream method.
  • 10:52 - 10:57
    And as before, the readStream method
    reads the response data
  • 10:57 - 11:01
    from the socket's input stream,
    and then returns the response,
  • 11:01 - 11:07
    as a single string.
    This time however, the HTTP URL connection
  • 11:07 - 11:14
    strips off the HTTP response headers
    and handles the error checking for you.
  • 11:15 - 11:19
    Now this string is then passed
    to the onPostExecute method
  • 11:20 - 11:23
    which displays the response
    in a text view.
  • 11:25 - 11:32
    The third class is Android HTTP client.
    This class is an implementation of the
  • 11:32 - 11:39
    Apache project's DefaultHttpClient and
    it allows a great deal of customization.
  • 11:39 - 11:44
    In particular, the class breaks
    an HTTP transaction into
  • 11:45 - 11:51
    a request object and into a response object.
    So you can create subclasses
  • 11:51 - 11:55
    that customize the handling of requests
    and their responses.
  • 11:56 - 11:59
    Now, by this point, you know
    what the application looks like,
  • 11:59 - 12:03
    so let's jump straight into the code
    and look at the implementation.
  • 12:05 - 12:12
    Now, here I've got the networking Android
    HTTP client application opened in the IDE.
  • 12:13 - 12:16
    Now, I'll open the main activity
    for this application.
  • 12:17 - 12:21
    And let's go right to the HTTP get task class.
  • 12:22 - 12:24
    That class begins by creating a new
  • 12:24 - 12:31
    AndroidHttpClient object by calling
    the classes newInstance method.
  • 12:32 - 12:36
    Now, when the doInBackground method
    is called, the code creates
  • 12:36 - 12:43
    an HttpGet object, passing in
    the URL string for that request.
  • 12:43 - 12:47
    Next, it creates a
    ResponseHandler object.
  • 12:47 - 12:54
    This object is responsible for handling
    the response to the HttpGet request.
  • 12:54 - 12:58
    In this case, the ResponseHandler
    is of type
  • 12:58 - 13:02
    basicResponseHandler,
    which will return the response's body.
  • 13:03 - 13:08
    Now we'll see a more complex
    ResponseHandler later in this lesson.
  • 13:09 - 13:12
    And finally, the request and the ResponseHandler
  • 13:13 - 13:15
    are passed into the execute method,
  • 13:15 - 13:19
    which sends the request,
    gets the response,
  • 13:20 - 13:22
    passing it through the ResponseHandler.
  • 13:23 - 13:25
    And the result of all this
  • 13:25 - 13:29
    is then passed on to onPostExecute,
  • 13:29 - 13:32
    which displays the response
    in a text field.
  • 13:32 - 13:36
    [BLANK_AUDIO]
Title:
Video Language:
English
  • Revision 1: former version of the English subtitles, based on the former version of the video

Metadata: Geo subtitles

Revisions