Return to Video

https:/.../index.mp4

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

English subtitles

Incomplete

Revisions