IOException when performing OutputStream.close() on Nokia 7650
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

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

Re: IOException when performing OutputStream.close() on Nokia 7650

Posted By:   Anonymous  
Posted On:   Monday, November 11, 2002 05:46 AM

There are 2 issues with the 7650 (one is not j2me related the other is).


1. You need to use an internet GPRS connection with the server. To do this set the WAP gateway IP in you access point details to 0.0.0.0.


2. Based on what I have heard on other boards, the HTTPConnection class may be a bit broken on the 7650 (something like a spelling mistake in HTTP/1,1) . I certainly couldn't get it to work. The way I got round it was not to use HTTPConnection but use StreamConnection with socket://. Like this:


StreamConnection conn = (StreamConnection)Connector.open("socket://(ip address of server):80");
// send request...
PrintStream out = new PrintStream(conn.openOutputStream());
out.println("POST /cgi-bin/your_script_here.pl HTTP/0.9");
out.println("Host: server_hostname_here");
out.println("Content-Type: application/x-www-form-urlencoded");

out.print("Content-Length: ");
out.println(postdata.length);
out.println();
out.println(postdata);
out.flush();
// get reply...
InputStream in = conn.openInputStream();
int ch;
while( ( ch = in.read() ) != -1 )
{
//System.out.print( (char) ch );
... }



Hope this helps.

Steve O'Connor
About | Sitemap | Contact