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


Obtaining Wireless News with J2ME and PHP : Page 2

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.

Client Side Implementation
On the client side (that is on the mobile device), the code must:

  1. Build a simple GUI that will:
    • Let users select the type of news they're interested in.
    • Display the list of titles of each item.
    • Show the complete item selected (title and description).
  2. Establish a connection with the Web server, providing it with a string representing the type of news the user wants to get.
  3. Get the actual news sent by the server.
  4. Parse the "raw" news received from the server and display the results in usable form.
The classes used to accomplish that are:

  • WirelessNews, responsible for the GUI.
  • NewsFetcher, responsible for connecting to the Web server and retrieving the news items.
  • NewsParser, which parses the "raw" news sent by the server.
  • News, which represents an item through its title and description
Starting with the simplest, here's the News class code:

   class News {
      String title;
      String description;
      public News(String title, String description) {
         this.title = title;
         this.description = description;
     public void setTitle(String title) {
         this.title = title;
      public void setDescription(String description) {
         this.description = description;
      public String getTitle() {
         return title;
      public String getDescription() {
         return description;
As you can see there isn't much to say about this class. It consists entirely of classic "getter" and "setter" methods.

The WirelessNews class
The WirelessNews class is more interesting. The code is shown in Listing 1.

As you can see from the screenshots in Figures 2, 3 and 4, the class needs to create and manage three screens. I called these screens channelsList, titlesList and fmDescription. The first screen shows the list of the available channels. In this context, by channel I mean a type of news, such as "World News", "US News" etc. The second screen shows the list of the titles of the news fetched by NewsFetcher (you'll see this class in detail later). The form fmDescription, displays individual item titles and descriptions.

Four commands, cmSelect, cmExit, cmBack, and cmMain handle interface interactions. The first, cmSelect, selects either the type of news to fetch or the title of the article the user wants to fully read. The command cmExit exits the application. The other two commands switch to the previous menu and the main menu respectively.

Since WirelessNews extends the MIDlet class, it must implement its three abstract methods, startApp, pauseApp and destroyApp, which are invoked when the application gets started, paused and destroyed, respectively. The startApp method sets channelsList as the current display. The most important method of this class is commandAction, which is the application event handler, invoked when a command is selected. This method is declared in the CommandListener interface. Here's the code for commandAction:

   public void commandAction(Command c, Displayable d) {
      //exit command
      if(c == cmExit) {
      //d == channelList
      if(d == channelsList) {
         //get the index of the channel selected
         int channelIndex = channelsList.getSelectedIndex();
         //create a class instance of the class to fetch the news
         NewsFetcher nf = new NewsFetcher(
           channels[channelIndex], this);
         //show an alert indicating we are starting a download
         showAlert("News Downloading..." + 
           "When done it will be displayed on the screen", d);
         //start the download
      //d == titlesList
      if(d == titlesList) {
         // back command
         if(c == cmBack)
         // select command. Show the description
         if(c == cmSelect)       showDescription((News)news.elementAt(
      //d == fmDescription
      if(d == fmDescription) {
         //back command
         if(c == cmBack)
         //main menu
         if(c == cmMain)  {
            titlesList = null;
The first thing this method does is check if the button pressed is cmExit, because if it is, the application has to exit, irrespective of which screen is currently active. All other actions, however, depend on the current screen. Because there are three different screens, there are three different cases to analyze in addition to cmExit:

  • Case A: The current screen is channelsList. This is a "special" case. It has only one active command—cmSelect. In this case the method retrieves the index of the selected channel and creates a NewsFetcher instance, passing in the string representing the channel and the current MIDlet. After that, it displays an alert to indicate that a download is starting, and launches the actual download by calling start on the NewsFetcher object. You'll see how NewsFetcher works later on this article. For the moment, it's sufficient to know that NewsFetcher performs the actual download of the news on a separate thread.
  • Case B: The current screen is titlesList. This screen has two options: either the user selected cmBack or cmSelect. For cmBack the method sets channelsList as the current display. For cmSelect the method invokes showDescription, passing in the selected news item. The showDescription method assigns the item's title and description to frmDescription and sets that as the current screen.
  • Case C: The current screen is fmDescription. The options here are cmBack and cmMain. The former switches to titlesList, the latter switches to channelsList.
The remaining method in the Wireless class is showTitles. The method accepts a Vector object representing a list of News objects, a Boolean representing the status of the download (success or failure) and a string representing the type of news. When the status is false, the method shows an alert displaying "bad" news, which is a download failure. When it's true, on the other hand, it builds a string array containing the titles of the news items just fetched. The method then instantiates titlesList, passing in (among other things) the titles to display. Finally the application displays the list of titles on the mobile device's screen.

The showAlert method doesn't requires any additional explanation, because it simply displays a standard alert message.

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