Problem servlet/midlet communication using HTTP in the device

Hi, I磎 trying to do a midlet communicating with a simple servlet, sending a string and then receiving the servlet response (as a string too), but when I try the application in a real device,a siemens cf75, it doen磘 work. After accepting the http connection, the application blocks in the waitscreen, showing the "wait for the http connection" message. I磛e tryed in the specific cf 75 emulator and works properly.

The method which opens the Http connection is:

private HttpConnection sendData(HttpConnection httpConn,OutputStream ouStr)throws IOException{

//we set the Http Connection

String url ="http://200.17.77.2:8082/Proyecto2BlueP/ServletProyecto2_1BP";

httpConn = (HttpConnection)Connector.open(url);

httpConn.setRequestMethod( HttpConnection.POST );

httpConn.setRequestProperty("User-Agent",

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

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

httpConn.setRequestProperty("Accept","application/octet-stream" );

httpConn.setRequestProperty("Connection","close");

httpConn.setRequestProperty("Content-Length",Integer.toString((DataEncoded.toString()).length()));

httpConn.setRequestProperty("Content-Type","application/octet-stream" );

ouStr = httpConn.openOutputStream();

DataOutputStream douStr =new DataOutputStream(ouStr);

//Data send (we transform the StringBuffer into a String)

douStr.writeUTF(DataEncoded.toString());

douStr.flush();

//We close the connection

ouStr.close();

return httpConn;

}

And the method which receives the data:

privatevoid connect(){

HttpConnection httpConn =null;

OutputStream ouStr=null;

boolean ret =false;

try{

//the code we have just seen:

httpConn=sendData(httpConn,ouStr);

//Now we reveive the data send by the servlet

InputStream inStr =null;

inStr = httpConn.openInputStream();

System.out.println("Hemos abierto el inputStream");

int contentLength = (int)httpConn.getLength();

int rc = httpConn.getResponseCode();

if( rc == HttpConnection.HTTP_OK ){

DataInputStream dinStr=new DataInputStream(inStr);

System.out.println("Nuevo Data Input Stream");

DataReceived=dinStr.readUTF(dinStr);

inStr.close();

httpConn.close();

}

else{

}

}

catch( IOException e ){

System.out.println("Error en la conexion!!!!");

System.out.println("IOException: "+e);

}

}

I磎 using netbeans 5.0. I dont know if the problem is because using openInputStream, or with the threads that are not well implented (in theory it磗 very intuitive to do int with the mobility pack and adding a simpleCancellableTask), or perhaps because the waitScreens. What can be the possible reasons of working in the emulator and not in the real device (it implements MIPD 2.0 and CLDC/1.1)?

Thanks a lot in advance!!!

[4500 byte] By [xaambea] at [2007-10-3 5:00:18]
# 1
what is the responseCode of the HttpConnection?
suparenoa at 2007-7-14 23:05:48 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2
Where exactly in your code does it hang?I'm not familiar with that device but i've seen cases where an emulator and device handle the http inputstream differently. Try using a simpler method for reading the response, ie don't use httpConn.getLength() and use inputstream.read()
dirtdoga at 2007-7-14 23:05:48 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
douStr.flush();try comment this line
sailesh_dita at 2007-7-14 23:05:48 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

I磛e tried to comment the "douStr.flush();" line in the midlet code, but if doesn磘 work yet.

In the servlet I磎 using the reception code:

public void receivedata(DataInputStream din) throws IOException{

DataEncodedRec=din.readUTF();

din.close();

}

And the send code is:

response.setStatus( response.SC_OK );

response.setContentType("application/octet-stream" );

ServletOutputStream out = response.getOutputStream();

DataOutputStream dout =new DataOutputStream(out);

dout.writeUTF(datatosend);

//dout.flush();

dout.close();

The dout.flush() is necessary in the servlet?

I磎 not using httpConn.getLength() reading method neither for the midlet and the servlet,but only the readUTF (even if I get the contentLength). It磗 the same using inputstream.read() and datainputstream.read() or not?

How can I know what is the response code of the HttpConnection when I磎 using the real device,doing a simple form with this response code?

Thanks.

xaambea at 2007-7-14 23:05:48 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

I磛e tried to comment the "douStr.flush();" line in the midlet code, but if doesn磘 work yet.

In the servlet I磎 using the reception code:

public void receivedata(DataInputStream din) throws IOException{

DataEncodedRec=din.readUTF();

din.close();

}

And the send code is:

response.setStatus( response.SC_OK );

response.setContentType("application/octet-stream" );

ServletOutputStream out = response.getOutputStream();

DataOutputStream dout =new DataOutputStream(out);

dout.writeUTF(datatosend);

//dout.flush();

dout.close();

The dout.flush() is necessary in the servlet?

I磎 not using httpConn.getLength() reading method neither for the midlet and the servlet,but only the readUTF (even if I get the contentLength). It磗 the same using inputstream.read() and datainputstream.read() or not?

How can I know what is the response code of the HttpConnection when I磎 using the real device,doing a simple form with this response code?

Thanks.

xaambea at 2007-7-14 23:05:48 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6
Usually when it works in th emulator and not on the real device, the problem is with the device, not the application (I said usually). So check to see if you can access the servlet from within your phone's browser. Mihai
Printisora at 2007-7-14 23:05:48 > top of Java-index,Java Mobility Forums,Java ME Technologies...