Posted By:
Marek_Blahus
Posted On:
Monday, September 30, 2002 12:39 PM
Hello. I have a problem with my simple J2ME application that I have tried to solve for several days, but with no success. I hope there is some one on this forum who could give me some help or suggestions. My MIDlet is designed to send an HTTP POST request to a web server, sending some data in the request's body to be stored by the server-side script. However, the server-side script is never run, as an IOException occurs on the Nokia 7650 phone I am using for testing already when trying to close the output stream ( os.close(); ). If I delete the os.close() line, the exception disappears, but the script on the server is not run. The same if I put os.flush() in place of os.close() . The MIDlet c
More>>
Hello. I have a problem with my simple J2ME application that I have tried to solve for several days, but with no success. I hope there is some one on this forum who could give me some help or suggestions.
My MIDlet is designed to send an HTTP POST request to a web server, sending some data in the request's body to be stored by the server-side script. However, the server-side script is never run, as an IOException occurs on the Nokia 7650 phone I am using for testing already when trying to close the output stream (
os.close();
).
If I delete the
os.close()
line, the exception disappears, but the script on the server is not run. The same if I put
os.flush()
in place of
os.close()
. The MIDlet code is as following:
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.String;
public class HTTPSubmitter extends MIDlet implements CommandListener, Runnable {
private Form mMainForm;
private StringItem mApplicationName;
private TextField mMessageField;
private Command mExitCommand, mPostCommand;
private Form mPostingForm;
private Form mPostedForm;
private Command mBackCommand;
private void postViaHttpConnection(String url, String posting) {
HttpConnection c = null;
OutputStream os = null;
// the request body
String requeststring = "msg=".concat(posting);
try {
c = (HttpConnection)Connector.open(url, Connector.WRITE);
// set the request method to POST
c.setRequestMethod(HttpConnection.POST);
// set the headers
c.setRequestProperty("Content-Length", "" + requeststring.length());
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
c.setRequestProperty("Content-Language", "en-US");
c.setRequestProperty("Accept", "*/*");
c.setRequestProperty("Connection", "close");
// obtain OutputStream for sending the request string
os = c.openOutputStream();
// send request string to server
os.write(requeststring.getBytes());
os.flush();
os.close();
}
catch (IOException e1) {
mPostedForm.append("Exception1: ".concat(e1.toString()));
}
finally {
if(os != null) {
try {
os.close();
}
catch(IOException e2) {
// e1.printStackTrace();
mPostedForm.append("Exception2: ".concat(e2.toString()));
}
}
if(c != null) {
try {
c.close();
}
catch(IOException e3) {
// e.printStackTrace();
mPostedForm.append("Exception3: ".concat(e3.toString()));
}
}
}
}
public HTTPSubmitter() {
mExitCommand = new Command("Exit", Command.EXIT, 1);
mPostCommand = new Command("Post", Command.SCREEN, 2);
mBackCommand = new Command("Back", Command.BACK, 2);
mMainForm = new Form("HTTP Submitter");
mApplicationName = new StringItem(null, "Posting System");
mMessageField = new TextField("Message:", null, 1000, TextField.ANY);
mMainForm.append(mApplicationName);
mMainForm.append(mMessageField);
mMainForm.addCommand(mPostCommand);
mMainForm.addCommand(mExitCommand);
mMainForm.setCommandListener(this);
mPostingForm = new Form("Posting...");
mPostingForm.append("Please wait.");
mPostingForm.setCommandListener(this);
mPostedForm = new Form("Posted.");
mPostedForm.append("Done.");
mPostedForm.addCommand(mBackCommand);
mPostedForm.addCommand(mExitCommand);
mPostedForm.setCommandListener(this);
}
public void startApp() {
Display.getDisplay(this).setCurrent(mMainForm);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s) {
if(c.getCommandType() == Command.SCREEN) {
Display.getDisplay(this).setCurrent(mPostingForm);
new Thread(this).start();
}
if(c.getCommandType() == Command.BACK) {
Display.getDisplay(this).setCurrent(mMainForm);
}
if(c.getCommandType() == Command.EXIT) {
notifyDestroyed();
}
}
public void run() {
postViaHttpConnection("http://www.myserver.com/j2me/HTTPSubmitter/post.php", mMessageField.getString());
Display.getDisplay(this).setCurrent(mPostedForm);
}
}
Please, anybody who has already a similar problem or has any suggestions on how to solve it, let me know. I will be thankful for any response.
Marek Blahus
blahus@seznam.cz
<<Less