java.lang.IllegalStateException

hi i get the following error: java.lang.IllegalStateException: Write attempted after request finished

i am trying to send data into a http connection but it wont allow me due to this error. any suggestions? thanks in advance and teh code is below:

server = (HttpConnection) Connector.open(url2);

is = server.openInputStream();

os = server.openOutputStream();

String username = details[0] +"\n" +"\0";

String pass = details[1] +"\n" +"\0";

os.write(username.getBytes());//error occurs here

os.flush();

os.write(pass.getBytes());

os.flush();

[742 byte] By [jonney69a] at [2007-11-27 2:58:40]
# 1
are you running multiple threads? are you accessing the "server" object on multiple threads?
pandora_fooa at 2007-7-12 3:38:22 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2
yes that above code is from a thread class but thats the only thread that tries to access the web server
jonney69a at 2007-7-12 3:38:22 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

Hi,

Try to set the request methods and header before you start writing

data .

server.setRequestMethod(HttpConnection.POST);

sercer.setRequestProperty("If-Modified-Since",

"29 Oct 1999 19:43:31 GMT");

server.setRequestProperty("User-Agent",

"Profile/MIDP-2.0 Configuration/CLDC-1.0");

server.setRequestProperty("Content-Language", "en-US");

Regards,

Rawad

Rawna at 2007-7-12 3:38:22 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

i have tried the above code and it diddnt work.

here is the revised code below

public void open()

{

try

{

server = (HttpConnection) Connector.open(url2);

server.setRequestMethod(HttpConnection.POST);

server.setRequestProperty("If-Modified-Since",

"29 Oct 1999 19:43:31 GMT");

server.setRequestProperty("User-Agent",

"Profile/MIDP-2.0 Configuration/CLDC-1.0");

server.setRequestProperty("Content-Language", "en-US");

is = server.openInputStream();

os = server.openOutputStream();

details();

/*

Alert alert = new Alert ("Error");

alert.setString("ok" + " " + data[0]);

alert.setTimeout (3000);

Display.getDisplay(root).setCurrent(alert);

//something = getResponse();

int gfdgf;

**/

}

catch(Exception e)

{

e.printStackTrace();

String error = e.toString();

Alert alert = new Alert ("Error");

alert.setString("Error, " + e);

alert.setTimeout (5000);

Display.getDisplay(root).setCurrent(alert);

}

}

details method that retreives data and stores into a array

public void details()

{

try

{

String username = details[0] + "\n" + "\0";

String pass = details[1] + "\n" + "\0";

os.write(username.getBytes());

os.flush();

os.write(pass.getBytes());

os.flush();

for(int i = 0; i < 20; i++)

{

data[i] = getResponse();

}

}

catch(Exception e)

{

e.printStackTrace();

String error = e.toString();

Alert alert = new Alert ("Error");

alert.setString("Error2, " + e);

alert.setTimeout (5000);

Display.getDisplay(root).setCurrent(alert);

}

}

and finaly the method that reads a line of data from the web server

public String getResponse()

{

StringBuffer b = new StringBuffer();

String read = "";

int ch = 0;

try

{

while(( (ch = is.read()) != '\n') )

{

b.append((char) ch);

}

read = b.toString();

b.setLength(0);

}

catch(IOException e)

{

e.printStackTrace();

read = "error";

}

return read;

}

jonney69a at 2007-7-12 3:38:22 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

IllegalStateException means that you are trying to put the thread on a state that is not valid from the current state. for exmple you are trying to start it when its already started. I guess your problem is not inside the thread code but on the class that is handling the thread.

on j2me there is no stop method. If you need to stop it and start it again, you should call the constructor again to do it. Thats what I do when I get that IllegalStateException.

MelGohana at 2007-7-12 3:38:22 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6
well from my main class all i do is just start the thread. i have another thread that deals with socket connections. could there be a clash between the two? cant i open a socket and http connection at the same time from one program?
jonney69a at 2007-7-12 3:38:22 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7
edit: i have tried implementing the thread class into a normal working class and i still have the same problem has before
jonney69a at 2007-7-12 3:38:22 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8
Well so you are calling start only one time! Are you sure? Maybe you are calling another method, try to find the line of code with the problem handling the exceptions and coding some console messages.
MelGohana at 2007-7-12 3:38:22 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9
i found out the problem. i got rid of the output streams and just sent the data via its actual url i am connecting to!for example http://java.com/login/?login=blah&pass=whatever
jonney69a at 2007-7-12 3:38:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 10

> i found out the problem. i got rid of the output

> streams and just sent the data via its actual url i

> am connecting to!

You didn't find our anything, you just made a workarround!

It really is very simple: you open both streams at ones. Opening the InputStream means that the outputstream is ready, and therefor you cannot write to it anymore, and therefore this exception is thrown.

deepspacea at 2007-7-12 3:38:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 11
yea i had to open the output stream first to write. in socket connections i can open both input/output. if i use the output stream will it generate the url similar to what i have sent ie sent the login and password details in the output stream
jonney69a at 2007-7-12 3:38:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...