Posting a form is a little more complicated on the MIDlet side. In particular, there are request headers that need to be set in HttpConnection before the server is contacted. The process works like this:
Obtain an HttpConnection from Connector's open() method.
Modify the header fields of the request. In particular, you need to change the request method by calling setRequestMethod(), and you should set the "Content-Length" header by calling setRequestProperty(). This is the length of the parameters you will be sending.
Obtain the output stream for the HttpConnection by calling openOutputStream(). This sends the request headers to the server.
Send the request parameters on the output stream returned from the HttpConnection. Parameters should be encoded as described earlier (and in the documentation for the J2SE class java.net.URLEncoder).
Read the response from the server from the input stream retrieved from HttpConnection's openInputStream() method.
The following example, Listing 9-2, demonstrates how to send a single parameter to a server using an HTTP POST. Multiple parameters can be assembled by joining them with an ampersand separator. Note that the parameter in this example has been encoded as described above. In this example, the parameter value "Jonathan Knudsen!" has been encoded to "Jonathan+Knudsen%21". Listing 9-3 shows a very simple servlet that can communicate with PostMIDlet.
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class PostMIDlet
extends MIDlet
implements CommandListener, Runnable {
private Display mDisplay;
private Form mForm;
public PostMIDlet() {
mForm = new Form("Connecting...");
mForm.addCommand(new Command("Exit", Command.EXIT, 0));
mForm.setCommandListener(this);
}
public void startApp() {
if (mDisplay == null) mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(mForm);
// Do network loading in a separate thread.
Thread t = new Thread(this);
t.start();
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT)
notifyDestroyed();
}
public void run() {
HttpConnection hc = null;
InputStream in = null;
OutputStream out = null;
try {
String message = "name=Jonathan+Knudsen%21";
String url = getAppProperty("PostMIDlet-URL");
hc = (HttpConnection)Connector.open(url);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
hc.setRequestProperty("Content-Length",
Integer.toString(message.length()));
out = hc.openOutputStream();
out.write(message.getBytes());
in = hc.openInputStream();
int length = (int)hc.getLength();
byte[] data = new byte[length];
in.read(data);
String response = new String(data);
StringItem stringItem = new StringItem(null, response);
mForm.append(stringItem);
mForm.setTitle("Done.");
}
catch (IOException ioe) {
StringItem stringItem = new StringItem(null, ioe.toString());
mForm.append(stringItem);
mForm.setTitle("Done.");
}
finally {
try {
if (out != null) out.close();
if (in != null) in.close();
if (hc != null) hc.close();
}
catch (IOException ioe) {}
}
}
}
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class PostServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String message = "Received name: '" + name + "'";
response.setContentType("text/plain");
response.setContentLength(message.length());
PrintWriter out = response.getWriter();
out.println(message);
}
}