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();
//username
String username = login.getUsername();
//password
String password = login.getPassword();
//title
String title = tfTitle.getString();
//text
String text = tfText.getString();
//retrieve the post
PostSender pr = new PostSender(midlet, this,
username, password, title, text);
pr.start();
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;
try
{
// 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
http.setRequestMethod(HttpConnection.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);
}
else
{
System.out.println("Network error");
networkError();
}
}
catch (Exception e)
{
System.err.println("Error: " + e.toString());
networkError(e.toString());
}
finally
{
if (is != null)
is.close();
if (sb != null)
result = sb.toString();
else
networkError();
if (http != null)
http.close();
}
if (result != "")
{
System.out.println(result);
midlet.showInfo(result, getCurrentDisplay());
}
else
{
networkError();
}
}
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 actionsthe MIDlet of the applicationin
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.