This chapter covers

■ Networking basics

■ Determining network status

■ Using the network to retrieve and store data

■ Working with web services

Every mobile provider supports voice and data networks of one or more types. The interesting part with an Android-enabled device is really the data network, along with the power to link the data available on the network to interesting applications. Those applications can then be built with the open Intent- and Service-based approach you learned about in previous chapters. That approach combines built-in (or custom) intents, such as fully capable web browsing, with access to hardware components, such as a 3D graphics subsystem, a GPS receiver, a camera, removable storage, and more. This combination of open platform, hardware capability, software architecture, and access to network data makes Android so compelling.

This is not to say that the voice network is not also important (and we will cover telephony explicitly in chapter 7), but rather it is simply an admittance that voice is almost a commodity, and data is where we will focus when talk about the network.

In terms of the data network, Android provides access in several ways: mobile Internet Protocol (IP) networks, Wi-Fi, and Bluetooth. Here we are going to concentrate on getting our Android applications to communicate using IP network data, with several different approaches. We will cover a bit of networking background, and then we will deal with Android specifics as we explore communication with the network using sockets and higher-level protocols such as Hypertext Transfer Protocol (HTTP).

Android provides a portion of the java.net package and the org.apache.http-client package to support basic networking. Other related packages such as android.net address internal networking details and general connectivity properties. We will encounter all of these packages as we progress though networking scenarios in this chapter.

In terms of connectivity properties, we will look at using the ConnectivityManager class to determine when the network connection is active and what type of connection it is (mobile or Wi-Fi). From there we will make use of the network in various ways with sample applications.

One caveat to this networking chapter is that we won't be digging into the details concerning the Android Wi-Fi or Bluetooth APIs. Bluetooth is an important technology for close-range wireless networking between devices, but the related Android APIs are not yet finalized (even in the 1.0 SDK). Bluetooth is supported on Android devices, but in a limited capacity at present, and is not available in the Android Emulator. Wi-Fi, on the other hand, does have a good existing API but also doesn't have an emulation layer. Because the emulator doesn't distinguish the type of network you are using and doesn't know anything about either Bluetooth or Wi-Fi, and because we think the importance lies more in how you use the network, we are not going to cover these APIs. If you want more information on the Wi-Fi APIs please see the Android documentation (http:// code.google.com/android/reference/android/net/wifi/package-summary.html).

Getting back to what we will address here, the aptly named sample application for this chapter, NetworkExplorer, will look at ways to communicate with the network in Android and will include some handy utilities. Ultimately this application will have multiple screens that exercise different networking techniques, as shown in figure 6.1.

After we cover general IP networking with regard to Android, we will discuss turning the server side into a more robust API itself by using web services. On this topic we will work with Plain Old XML over HTTP (POX) and Representational State Transfer (REST). And, we will discuss the Simple Object Access Protocol (SOAP). We will address the pros and cons of the various approaches and why you might want to choose one method over another for an Android client.

Before we delve into the details of networked Android applications, we will begin with an overview of networking basics. If you are already well versed in general networking, you can certainly skip ahead to section 6.2, but it is important to have this foundation if you think you need it, and we promise to keep it short.

Enter some data and press SOCKET to submit to socket (server must be listening for this to work)

APACHE VIA HELPER

HTTP HELPER FORM

DELICIO-USLOOINsnd POSTS

GOOGLE CLIENTLOGIN and TOKEN

Status Output:

Networkinfo: type: MOBILE, state: CONNECTED/CONNECTED, reason: (un extra: Internet, fall over: false. IsAvall

Enter some data and press SOCKET to submit to socket (server must be listening for this to work)

APACHE VIA HELPER

HTTP HELPER FORM

DELICIO-USLOOINsnd POSTS

GOOGLE CLIENTLOGIN and TOKEN

Status Output:

Networkinfo: type: MOBILE, state: CONNECTED/CONNECTED, reason: (un extra: Internet, fall over: false. IsAvall

I Androiod Rocksj

Fill in url, method, opti user/pass, and click GO

[http://192.168.0. .

Flit in email, then select GET TOKEN, and then GET CONTACTS (must have token before get contacts will work).

Google Email Address:

I [email protected]

Figure 6.1 The NetworkExplorer application we will build to cover networking topics

I Androiod Rocksj

[http://192.168.0. .

Flit in email, then select GET TOKEN, and then GET CONTACTS (must have token before get contacts will work).

Google Email Address:

I [email protected]

Figure 6.1 The NetworkExplorer application we will build to cover networking topics

0 0

Post a comment