I have an HTTP connection, opened by
我有一个HTTP连接,打开
HttpConnection c = (HttpConnection)Connector.open(url);
where url
is one of:
url是以下之一:
http://foo.bar;deviceside=false
http://foo.bar;deviceside=false;ConnectionType=mds-public
http://foo.bar;deviceside=true;ConnectionUID=xxxxxxx
http://foo.bar;deviceside=true;interface=wifi
Is there any way to cause the request to error out immediately if the connection cannot be established because the device is not connected to a network? As it is, it takes about a minute to timeout in many cases (specifically on the first call to get the information from the network: c.getResponseCode()
)
如果由于设备未连接到网络而无法建立连接,是否有任何方法可以立即导致请求出错?实际上,在许多情况下需要大约一分钟才能超时(特别是在第一次从网络获取信息时:c.getResponseCode())
Edit: I mean error out. In one case, Wifi, specifically, it will sit around for several minutes if the wifi is not on before timing out, and I want it to stop right away.
编辑:我的意思是错误。在一个案例中,Wifi,具体来说,如果在超时之前没有打开wifi,它将会坐几分钟,我希望它立即停止。
4 个解决方案
#1
I use the RadioInfo class to check if there is a connection and if the radio is turned on before trying to make a connection. Then you can just display a message to the user or turn the radio on (if it's off) before trying to connect, makes for a much better user experience.
我使用RadioInfo类来检查是否存在连接以及在尝试建立连接之前是否打开了无线电。然后,您可以在尝试连接之前向用户显示消息或打开无线电(如果已关闭),从而获得更好的用户体验。
Try using:
if (RadioInfo.getState() == RadioInfo.STATE_OFF)
OR
if (RadioInfo.getSignalLevel() == RadioInfo.LEVEL_NO_COVERAGE)
To check connection status before connecting.
在连接之前检查连接状态。
#2
I encase my posts in a thread to timeout faster. Make sure your "PostThread" catches all exceptions (and saves them).
我将帖子包含在一个线程中以更快地超时。确保你的“PostThread”捕获所有异常(并保存它们)。
public byte[] post(String url, byte[] requestString){
PostThread thread=new PostThread(url, requestString);
synchronized(thread){
try{
thread.start();
thread.wait(TIMEOUT);
}catch(Throwable e){
}//method
}//synch
if (thread.isAlive()){
try{
thread.interrupt();
}catch(Throwable e){
}//method
D.error("Timeout");
}//endif
if (thread.error!=null) D.error(thread.error);
if (thread.output!=null) return thread.output;
throw D.error("No output");
}//method
There is also the ConnectionTimeout
parameter, which I have not tested: eg socket://server:80/mywebservice;ConnectionTimeout=2000
还有ConnectionTimeout参数,我还没有测试过:例如socket:// server:80 / mywebservice; ConnectionTimeout = 2000
#3
Not any way that can be specified programmatically. It can be irritating, but a connection from a mobile device - especially a BlackBerry - generally goes through a few different networks and gateways before reaching the destination server: wireless->Carrier APN->Internet->BES (maybe)->foo.bar server so a large timeout is built-in to account for potential delays at any of those points.
没有任何可以通过编程方式指定的方式。这可能很烦人,但来自移动设备(尤其是BlackBerry)的连接通常会在到达目标服务器之前通过几个不同的网络和网关:无线 - >运营商APN->互联网 - > BES(可能) - > foo。条形码服务器因此内置了一个大的超时以解决任何这些点的潜在延迟。
You can control default device connection timeout from your BES/MDS server (or in the JDE, from the MDS\config\rimpublic.property file) - but that probably won't help you.
您可以从BES / MDS服务器(或在JDE,MDS \ config \ rimpublic.property文件中)控制默认设备连接超时 - 但这可能对您没有帮助。
#4
It would be better to have a Timeout check from a different thread, Because this is gonna happen even when the connection is established, say the network latency is very high, so u dont want the user to wait for so long or such thing.
从不同的线程进行超时检查会更好,因为即使建立连接也会发生这种情况,比如网络延迟非常高,所以你不希望用户等待这么久或者这样的事情。
So, in that case have a check from a different thread, whether the current time minus time entered for initiating the connection is more than your set time, close the connection using connection.close()!
因此,在这种情况下,从另一个线程进行检查,当前时间减去为启动连接而输入的时间是否超过设定时间,请使用connection.close()关闭连接!
#1
I use the RadioInfo class to check if there is a connection and if the radio is turned on before trying to make a connection. Then you can just display a message to the user or turn the radio on (if it's off) before trying to connect, makes for a much better user experience.
我使用RadioInfo类来检查是否存在连接以及在尝试建立连接之前是否打开了无线电。然后,您可以在尝试连接之前向用户显示消息或打开无线电(如果已关闭),从而获得更好的用户体验。
Try using:
if (RadioInfo.getState() == RadioInfo.STATE_OFF)
OR
if (RadioInfo.getSignalLevel() == RadioInfo.LEVEL_NO_COVERAGE)
To check connection status before connecting.
在连接之前检查连接状态。
#2
I encase my posts in a thread to timeout faster. Make sure your "PostThread" catches all exceptions (and saves them).
我将帖子包含在一个线程中以更快地超时。确保你的“PostThread”捕获所有异常(并保存它们)。
public byte[] post(String url, byte[] requestString){
PostThread thread=new PostThread(url, requestString);
synchronized(thread){
try{
thread.start();
thread.wait(TIMEOUT);
}catch(Throwable e){
}//method
}//synch
if (thread.isAlive()){
try{
thread.interrupt();
}catch(Throwable e){
}//method
D.error("Timeout");
}//endif
if (thread.error!=null) D.error(thread.error);
if (thread.output!=null) return thread.output;
throw D.error("No output");
}//method
There is also the ConnectionTimeout
parameter, which I have not tested: eg socket://server:80/mywebservice;ConnectionTimeout=2000
还有ConnectionTimeout参数,我还没有测试过:例如socket:// server:80 / mywebservice; ConnectionTimeout = 2000
#3
Not any way that can be specified programmatically. It can be irritating, but a connection from a mobile device - especially a BlackBerry - generally goes through a few different networks and gateways before reaching the destination server: wireless->Carrier APN->Internet->BES (maybe)->foo.bar server so a large timeout is built-in to account for potential delays at any of those points.
没有任何可以通过编程方式指定的方式。这可能很烦人,但来自移动设备(尤其是BlackBerry)的连接通常会在到达目标服务器之前通过几个不同的网络和网关:无线 - >运营商APN->互联网 - > BES(可能) - > foo。条形码服务器因此内置了一个大的超时以解决任何这些点的潜在延迟。
You can control default device connection timeout from your BES/MDS server (or in the JDE, from the MDS\config\rimpublic.property file) - but that probably won't help you.
您可以从BES / MDS服务器(或在JDE,MDS \ config \ rimpublic.property文件中)控制默认设备连接超时 - 但这可能对您没有帮助。
#4
It would be better to have a Timeout check from a different thread, Because this is gonna happen even when the connection is established, say the network latency is very high, so u dont want the user to wait for so long or such thing.
从不同的线程进行超时检查会更好,因为即使建立连接也会发生这种情况,比如网络延迟非常高,所以你不希望用户等待这么久或者这样的事情。
So, in that case have a check from a different thread, whether the current time minus time entered for initiating the connection is more than your set time, close the connection using connection.close()!
因此,在这种情况下,从另一个线程进行检查,当前时间减去为启动连接而输入的时间是否超过设定时间,请使用connection.close()关闭连接!