I'm writing one application in which I'm sending post request to server. The problem is that Nokia S40 series phone sends 2 request if it doesn't get response within 30 sec. I have checked the same with s60 series phone in my app is working fine.
One solution I found is to start timer while opening connection and terminate the connection after 25sec if connection not getting response. But it is also worthless. Here I'm attaching my code. Please review it and help me if possible.
try
{
param="function=CloseRecharge&LoginId="+SharedVariable.getUserInfo().getLoginID()
+"&BatchId="+SharedVariable.getSelectedProduct().getBatchID()
+"&SystemServiceID="+SharedVariable.getSelectedProduct().getSystemServiceID()
+"&ReferalNumber="+strMobileNo
+"&FromANI="+fromMoNo
+"&Email="+""
+"&Checksum="+Checksum;
connection = (HttpConnection) Connector.open(url);
timeout=0;
t.schedule(new TimerTask()
{
public void run()
{
try
{
timeout+=1;
System.out.println("Timeout value:"+timeout);
if(timeout>=25)
{
//timeout every 1 munite
timeout=0;
System.out.print("Connection is closing");
connection.close();
connection=null;
if(is!=null)
is.close();
Alert alert=new Alert("HttpConn Closed", "connection closed",null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert,new MainMenu("Menu", parent));
this.cancel();
}
}
catch(Exception e)
{
System.out.println("========Exception occured in Timer Task");
e.printStackTrace();
}
}
}
, 0, 1000);
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
connection.setRequestProperty("Accept_Language","en-US");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-length", ""+param.getBytes().length);
out = connection.openOutputStream();
out.write(param.getBytes());
out.flush();
param=null;
if (connection.getResponseCode() == HttpConnection.HTTP_OK )
{
t.cancel();
int len = (int)connection.getLength();
InputStream istrm = connection.openInputStream();
if (istrm == null)
{
throw new IOException("Cannot open HTTP InputStream, aborting");
}
if (len != -1)
{
data = new byte[len];
int bytesRead = istrm.read(data);
}
else
{
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int ch;
int count = 0;
while ((ch = istrm.read()) != -1)
{
bo.write(ch);
count++;
}
data = bo.toByteArray();
bo.close();
}
String response = new String(data);
displayResponse();
}//if
else
{
Alert error=new Alert("ResponseCode:"+connection.getResponseCode(),
null,
null,AlertType.INFO);
error.setTimeout(Alert.FOREVER);
display.setCurrent(error,new MainMenu("Menu", parent));
}
catch(Exception exception)
{
displayErrorMessage(exception);
}
finally
{
try
{
if(connection!=null)
connection.close();
if(is!=null)
is.close();
}
catch (IOException ex)
{
System.out.println("Exception in Finally block....");
ex.printStackTrace();
}
}