Using HTTP Networking

The most common network transfer protocol is Hypertext Transfer Protocol (HTTP). Most commonly used HTTP ports are open and available for use on phone networks.

A fast way to get to a network resource is by retrieving a stream to the content. Many Android interfaces for reading data accept streams. One such example is XmlPullParser. The setInput() method of XmlPullParser class takes an InputStream. Previously, you retrieved this stream from the resources. Now, however, you can get it from a network resource, using the simple URL class, as shown here:

URL xmlUrl = new URL(xmlSource); XmlPullParser questionBatch =

XmlPullParserFactory.newInstance().newPullParser(); questionBatch.setInput(xmlUrl.openStream(), null);

From here, the parsing of the XML is unchanged because the XML format is the same, and the XmlResourceParser used previously was derived from the XmlPullParser class. Because the parsing may take longer now, you will move it off the main thread later in the hour.

Once you have the question batches and score data downloading from the remote server, you can remove the mock XML resources from the project and the code that retrieves the XML resources.

Did you Know?

If your application wants to retrieve and display web content, you can use the WebView control, which leverages the WebKit rendering engine to draw HTML content onscreen. You can use the WebView control to display local or remote content.

0 0

Post a comment