Java HttpConnection在30秒后发送另一个请求
我正在编写一个应用程序,其中我正在向服务器发送post请求。问题在于,如果诺基亚S40系列手机在30秒内未收到响应,则会发送2个请求。我已经在我的应用程序中检查了s60系列手机是否正常工作 我发现的一个解决方案是在打开连接时启动计时器,如果连接没有得到响应,则在25秒后终止连接。但它也毫无价值。这里我附上我的代码。请检查一下,如果可能的话请帮助我Java HttpConnection在30秒后发送另一个请求,java,http,java-me,j2mepolish,nokia-s40,Java,Http,Java Me,J2mepolish,Nokia S40,我正在编写一个应用程序,其中我正在向服务器发送post请求。问题在于,如果诺基亚S40系列手机在30秒内未收到响应,则会发送2个请求。我已经在我的应用程序中检查了s60系列手机是否正常工作 我发现的一个解决方案是在打开连接时启动计时器,如果连接没有得到响应,则在25秒后终止连接。但它也毫无价值。这里我附上我的代码。请检查一下,如果可能的话请帮助我 try { param="fu
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();
}
}
您是否尝试过使用Connector.open(url,Connector.READ\u WRITE,true/*timeout*/)代替Connector.open(url)?是的。我试过了。但它也不起作用。它只接受真值或假值。