kohhworlwide kohhworlwide - 19 days ago 5
HTTP Question

HTTP Get Request - Program Stops

I'm developing a javaME project in which I'm reading a variable from a device and updating it in a Website. Basically, in Eclipse, the app runs perfectly. But when running from my java modem(code runs much faster) it gets stuck after running for one hour. I have done some tests and the program stops at the line in which I try to get the response code. So, am I building the method incorrectly? Should I make anything different?

void enviarPost(int i, String comando)throws IOException, IllegalStateException, IllegalArgumentException, ATCommandFailedException{

System.out.println("Connecting to websitedummy.com...");
if(i == 1)
{
url = "http://websitedummy.com/index.php?IMEI=" + imeiX + "&IP=" + ipX;
}
//53543D303B44723D4E616F
else
{
System.out.println("Atribuir url2");
url2 = comando;
url = "http://websitedummy.com/index.php?data={\"IMEI\":\""+imeiX+"\",\"TS\":\"20/04/13-08:31:44\",\"SER\":\""+url2+"\"}";

System.out.println("Atribuiu: "+ url2);
}



try {
Thread.sleep(1000);
System.out.println("Done1");
connection = (HttpConnection) Connector.open(url);
System.out.println("Done2");
Thread.sleep(500);
connection.setRequestMethod(HttpConnection.GET);
System.out.println("Done3");
Thread.sleep(500);
connection.setRequestProperty("Content-Type", "text/plain");
System.out.println("Done4");
Thread.sleep(500);
connection.setRequestProperty("Connection", "close");
System.out.println("Done5");
Thread.sleep(500);
int con = connection.getResponseCode();
Thread.sleep(500);
System.out.println(con);
if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
System.out.println("Vamos6");
inputstream_ = connection.openInputStream();
int ch;
while ((ch = inputstream_.read()) != -1 ) {
dataReceived.append((char) ch);

}
System.out.println("Updated");
acabouatualizar=1;
connection.close();
System.out.println("Closed");
} else {
System.out.println("Error");
// Connection not ok
}
} catch (Exception e) {
System.out.println(e);

} finally {
if (inputstream_ != null) {
try {

inputstream_.close();

} catch (Exception e1) {
System.out.println( e1);
}
}
if (connection == null) {
try {
System.out.println("Connection closed");
connection.close();

} catch (Exception e2) {
System.out.println(e2);
}
}
}
}


The program stops and doesn't present any type of exception which makes me think it only freezes. The last output I can see is "Done5".

Edit: Made the modem reset at every hour. Avoided the problem, not resolved it.
Anyone? :)

Many thanks in advance.

Answer

Turns out the problem was caused by this line

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

Removed it and runs like a charm. Solved.