wood burning stoves 2.0*
The moose likes Java Micro Edition and the fly likes Problem with openDataInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Problem with openDataInputStream" Watch "Problem with openDataInputStream" New topic
Author

Problem with openDataInputStream

Alberto Alvarez
Greenhorn

Joined: Dec 18, 2001
Posts: 4
Hi everybody,
my problem is that when i establish an Http connection from my midlet to a servlet which has to perform various operations before returning the response, the application seems to hang up (the midp.exe process consumes up to 99% of the machine�s CPU).
I �ve been testing the app and when the openDataInputStream method is invoked, the problem comes up. The app "stops" here until all the data is received, and the stream is closed.
If the same url is invoked from my MS Explorer, i have no problem with the servlets and other Java classes which are executed (here there is no stream opened).
Does anybody know how can I solve this problem, because all my app is slowed down because of this problem.
Thanks.
liang gu
Ranch Hand

Joined: Nov 05, 2001
Posts: 89
Are you testing on the simulator or on the actual device(and what kind of device)? Can you post your code? If you are testing on the device, can you also tell which device you are using and which way you are monitoring the application's resource consumption?
Liang
Alberto Alvarez
Greenhorn

Joined: Dec 18, 2001
Posts: 4
Hi,
I�m testing the app on the simulator and I�m using the latest version of the j2mewkt 1.0.3 with jdk 1.3.1
To monitorize the process I�m using window�s task administrator. Here is my code sample:

public String sendGetRequestOutput()
{
//variables
String parametros="";
DataInputStream iStrm =null;

//sacamos parametros de la hash para montar la url
try
{
if (htParam != null)
{
Enumeration eClaves = htParam.keys();

while (eClaves.hasMoreElements())
{
String clave = (String)eClaves.nextElement();
String value = (String)htParam.get(clave);

parametros += clave + "=" + value + "&";

}
parametros = parametros.substring(0,parametros.length()-1);
}//if
}// try
catch (Exception e)
{
System.out.println("[SetHttpConnection] Error al extraer datos de tabla hash: " +e);
}

if (url.equals(""))
{
url=URL;
}

try
{
url += servletName + "?" + parametros;

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

http.setRequestMethod(HttpConnection.GET);

http.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0" );

http.setRequestProperty("Accept","text/plain");

iStrm = http.openDataInputStream();

if (http.getResponseCode()== HttpConnection.HTTP_OK)
{
StringBuffer b = new StringBuffer();
int ch;
while ((ch = iStrm.read()) != -1)
{

b.append((char) ch);
}
return b.toString();
}
else
{


return null;
}
}//try
catch(Exception e)
{
return null;
}
finally
{
try
{
if (http != null)
{
http.close();
}
if (iStrm != null)
{
iStrm.close();

}
}
catch(Exception e)
{
System.out.println("[SetHttpConnection] Error al cerrar conexion http " +e);
}


}

}
Please help me,
thanks.
Alberto Alvarez
Greenhorn

Joined: Dec 18, 2001
Posts: 4
Sorry, in the j2mewkt 1.0.3 the process is called zayit.exe
liang gu
Ranch Hand

Joined: Nov 05, 2001
Posts: 89
Ok, first I like to have a clear understanding of the problem you are facing. Are you saying that: the HTTP connection from MIDLet to servlet takes a very long time, while with the same URL the connection time from IE browser to servlet is much less?
If this is the case, I will suggest you to read the input stream into a big buffer instead of reading it one character by one character. I have experienced dramatic difference in HTTP connection time.
Alberto Alvarez
Greenhorn

Joined: Dec 18, 2001
Posts: 4
Yes, you understood correctly the question. Now my doubt is how do I manage with a big buffer, rather than reading character by character? I would be very pleased if you showed me an example.
By the way, I have deduce that the problem is the openDataInputStream�s implementation. I think it falls in a infinitive loop, while waiting for data. This is the cause of the process�s CPU consume problem. My problem is that if this is true, I don�t know how to solve it.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with openDataInputStream