views:

1590

answers:

4

I have an HTTP connection, opened by

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

where url is one of:

  • 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())

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.

A: 

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.

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.

Anthony Rizk
+3  A: 

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.

Try using:

if (RadioInfo.getState() == RadioInfo.STATE_OFF)
OR
if (RadioInfo.getSignalLevel() == RadioInfo.LEVEL_NO_COVERAGE)

To check connection status before connecting.

Fermin
A: 

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()!

Azlam
A: 

I encase my posts in a thread to timeout faster. Make sure your "PostThread" catches all exceptions (and saves them).

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'

Kyle Lahnakoski