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


Build Brilliant Client/Server Apps with J2ME, PHP, and MySQL : Page 3

In this article you'll see how to make the three technologies work together by developing a simple and useful application that lets you update a multi-user blog and download the latest posts from it.

Writing and Uploading Posts
Figure 5 shows the form used to write a post. To send a post you enter a title and the post text, then select the Upload command, which runs this code:

   //retrieve the login data
   Login login = new Login();
   String username = login.getUsername();
   String password = login.getPassword();
   String title = tfTitle.getString();
   String text = tfText.getString();
   //retrieve the post
   PostSender pr = new PostSender(midlet, this, 
      username, password, title, text);
This code retrieves the user's credentials plus the title and text of the post and sends this data to the server using a separate thread invoked by the PostSender class' start method. The core method of PostSender is sendPost:

   private void sendPost() throws IOException
      InputStream is = null;
      StringBuffer sb = null;
      HttpConnection http = null;
         // append the query string onto the URL
         URL += "?username=" + username + "&password=" + password + 
            "&title=" + title + "&text=" + text;
         // replace chars not allowed in the URL
         URL = EncodeURL(URL);
         // establish the connection
         http = (HttpConnection) Connector.open(URL);
         // set the request method as GET
         // server response
         if (http.getResponseCode() == HttpConnection.HTTP_OK)
            sb = new StringBuffer();
            int ch;
            is = http.openInputStream();
            while ((ch = is.read()) != -1)
               sb.append((char) ch);
            System.out.println("Network error");
      catch (Exception e)
         System.err.println("Error: " + e.toString());
         if (is != null)
         if (sb != null)
            result = sb.toString();
         if (http != null)
      if (result != "")
         midlet.showInfo(result, getCurrentDisplay());
The sendPost method establishes a connection with the server, sends the data needed to store the post and waits for a response, which can be either a success or failure notification. The URL string for this example is http://localhost:8000/Blog/post_retriever.php, where 8000 is the port used for Apache Web Server and post_retriever is the PHP script responsible for storing the post data in the database. You'll see this in due course when analyzing the server-side application.

Author's Note: Be sure to change the URL shown in the preceding to match your settings.

Figure 7. Obtaining a post: Here's a downloaded post as displayed within a form on the device.
Downloading Posts
I'm not going to examine the post-retrieval feature because the code is very similar to that just shown for sending the post, but you can see the complete code that builds the main menu GUI and responds to user actions—the MIDlet of the application—in Listing 1. The main difference is that, to retrieve a post, the application sends the user's username and password plus the username of the post's author. The response will be the latest post uploaded by that author. The post downloaded is in a CSV-like form, using the pipe character as a separator, for example:

   alessandro|2006-06-28 10:51:23|First Title|First Text
You might also use an XML-like format similar to the one used in my previous article.

In the preceding sample response, alessandro is the username of the post's author followed by the posting date, title, and text. Note that if you intend to allow the pipe character within posts you should use a different separator or some other technique to distinguish your fields.

Figure 7 shows the form displayed by the application when you download a post.

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