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]

# 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 >

# 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;
}
# 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.