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


Obtaining Wireless News with J2ME and PHP : Page 4

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.

The NewsParser Class
The server returns "raw" news to the mobile device, which must be parsed to extract the titles and descriptions. NewsParser is the class responsible for doing that. Here's the code:

   class NewsParser {
      private String rawNews;
      private int index;
      public NewsParser(String rawNews) {
         this.rawNews = rawNews;
         this.index = 0;
      public Vector parse() {
         Vector v = new Vector();
         if(rawNews =="")
            return v;
         //extract the news
         while(index < rawNews.length()) {
            //extract the title
            String title = extractText(index, "t");
            //extract the description
            String description = extractText(index, "d");
            News news = new News(title, description);
         return v;
      //extract the text (a title or a description 
      //depending on the type parameter)
      private String extractText(int beginIndex, String type) {
         String startTag = "<" + type + ">";
         String endTag = "</" + type + ">";
         //find the index of startTag in rawNews, starting      
         //from beginIndex
         int begin = rawNews.indexOf(startTag, beginIndex);
         //move 3 chars ahead to point to the beginning of 
         //the actual text (title or descr.)
         begin += 3;
         //find the index of endTag in rawNews, 
         //starting from begin
         int end = rawNews.indexOf(endTag, begin);
         //update index
         index = end + 4;
         //return the actual text representing a title 
         //or a description
         return rawNews.substring(begin, end);
The parse method performs the actual parsing. This method returns a Vector of News objects. NewsParser has two fields: rawNews and index. The former is the string representing the news downloaded from the server. The latter represents a pointer to the character position within rawNews where the parse method begins looking for the next title and description. Here's an example to clarify. Suppose rawNews is:

   <t>A new article has been published on DevX.com</t><d>This 
   article deals with J2ME, PHP and their interaction</d><t>Funny 
   saying</t><d>There are only 10 types of people in the world: 
   those who understand binary and those who don't!</d>...
When you instantiate a NewsParser instance, index points to the first char of rawNews, which is "<". When the parse method gets called, it extracts titles and descriptions until it reaches the end of rawNews. It accomplishes this using a helper method, extractText, which receives beginIndex and type as parameters. The first represents the starting character index, which is constantly updated as the method moves through the raw input. The type parameter represents the type of text to extract: "t" for title, or "d" for description. The extractText method extracts the text, updates index to the new value and returns the extracted text. For each title and description extracted, parse creates a News object and adds it to the vector. Finally it returns the vector to the getNews method.

This article has described how J2ME and PHP can interact. As a proof of concept, you have seen how to build a pretty useful application that allows a user to get the latest news from the Web. Of course, you could improve the simple sample project, for example by storing the information retrieved from the Web on the mobile device using RMS (Record Management System). But that's another story.

Alessandro Lacava is a software developer and technical writer based in Italy. He is mainly interested in Java and .NET technologies, Web applications and telecommunication systems. Alessandro holds a degree in telecommunications engineering. You can reach him via his Web site.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date