RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Obtaining Wireless News with J2ME and PHP

J2ME and PHP are both powerful technologies used to develop applications for mobile devices and Web applications, respectively. In this article, you'll see how to make them work together by developing a simple and useful application that retrieves the latest news from the Web and displays that on a mobile device.

he Java 2 Platform, Micro Edition (J2ME) is targeted at cell phones, smart cards, pagers, and other consumer devices. J2ME technology consists of a virtual machine and a set of APIs suitable for tailored runtime environments for these devices. PHP, on the other hand, is a widely used server-based language to build Web applications. But these two radically different technologies work very well together. In this article you'll see how they can interact via HTTP (Hyper Text Transfer Protocol). Of course, this is not intended to be a thorough explanation of how HTTP works—you only need to know that HTTP is a request/response protocol. That simply means that the client application performs a request and the server application returns a response.

Figure 1 illustrates a typical HTTP-based Client/Server communication. In this case, the client requests the latest news to a Server sending it the type of news wanted. The Server simply responds to the Client sending it the news requested.

You understand then, that as long as client and server "speak" the same protocol they can communicate without problems. HTTP is the ad hoc protocol for our purposes. In fact, HTTP doesn't specify which entity is supposed to be either the client or the server. The client can be a Web browser, a mobile device, or anything else, provided that the applications on both ends can communicate through HTTP. For example, suppose you write something like this in PHP:

Figure 1. Client/Server Communication: The figure illustrates the process by which a client requests and receives news from a server.

   echo "Hello World";

The result depends on the client performing the request. If the client is a Web browser then it will display the string "Hello World." If the client is a mobile phone it will just receive the string "Hello World" as a stream of bytes from the server.

The point is that it's not hard to get a mobile device, running J2ME to communicate with a server running PHP as long as both use the HTTP protocol.

A Sample Project
As I'm sure many of you developers out there are, I'm fed up with parsing RSS feeds to get information and links. RSS feeds are nothing more than XML files with a particular structure. They are used mainly to represent a set of items published on a Web site. Here's an RSS feed excerpt:

      <title>The title of the first article</title>
      <description>The description of the first article</description>
      <link>The link to the full article</link>
      <title>The title of the second article</title>
Each item has title and description elements as well as link elements that point to the full article online (and other elements that are of no interest for this project).

In this article I'll show you how to design and implement an application for mobile devices that retrieves the latest news from some Web sites that offer RSS feeds as a service. The application will fetch the title and the corresponding description of each article of the RSS feed.

The following images serve to clarify the intent and overall functionality of the application.

Figure 2. Available Channels: The first screen lists the available news channels.

Figure 3. News Item Titles: After selecting a channel, you'll see a list of titles for the type of news chosen.

Figure 4. Article Description: After selecting a title from the list, you'll see the complete description of the selected article.

I won't discuss the server side of the application in detail because it just parses the RSS feed—you can find numerous tutorials and articles that explain that process, and both the server PHP code and the client J2ME code are included in the sample code that accompanies this article. The only thing that matters for this article is that the server-side PHP script fetches the titles and the descriptions from the news Web sites (www.nytimes.com and www.bbc.co.uk), and returns them in this fashion:

   <t>The title of the first article</t>
   <d>The description of the first article</d>
   <t>The title of the second article</t>
   <d>The description of the second article</d>
There are two main reasons for "shrinking" the news feeds this way. First, reducing the number of bytes downloaded from the Web server decreases download costs for clients who pay by byte volume for their mobile devices (which is often the case). Second, the less data you download, the faster the application will be.

Of course, the client side is responsible for restoring the information in a convenient fashion.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date