如果没有可用的连接,黑莓HTTP请求可以立即报错吗?

时间:2009-05-05 15:03:08

标签: http blackberry java-me

我有一个HTTP连接,由

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

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

如果由于设备未连接到网络而无法建立连接,是否有任何方法可以立即导致请求出错?实际上,在许多情况下需要大约一分钟才能超时(特别是第一次从网络获取信息的呼叫:c.getResponseCode()

编辑:我的意思是错误。在一个案例中,Wifi,具体来说,如果在超时之前没有打开wifi,它将会坐几分钟,我希望它立即停止。

4 个答案:

答案 0 :(得分:6)

我使用RadioInfo类来检查是否存在连接,以及在尝试建立连接之前是否打开了无线电。然后,您可以在尝试连接之前向用户显示消息或打开无线电(如果已关闭),从而提供更好的用户体验。

尝试使用:

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

连接前检查连接状态。

答案 1 :(得分:1)

我将帖子包含在一个线程中以更快地超时。确保你的“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

还有ConnectionTimeout参数,我还没有测试过:例如socket://server:80/mywebservice;ConnectionTimeout=2000

答案 2 :(得分:0)

不能以编程方式指定任何方式。这可能很烦人,但是来自移动设备(尤其是BlackBerry)的连接通常在到达目标服务器之前经过几个不同的网络和网关:无线 - >运营商APN->互联网 - > BES(可能) - > foo.bar服务器,因此内置了一个大的超时,以解决这些点中任何一点的潜在延迟。

您可以从BES / MDS服务器(或在JDE,MDS \ config \ rimpublic.property文件中)控制默认设备连接超时 - 但这可能对您没有帮助。

答案 3 :(得分:0)

从不同的线程进行超时检查会更好,因为即使建立连接也会发生这种情况,比如网络延迟非常高,所以你不希望用户等待这么久或者这样的事情。

因此,在这种情况下,从另一个线程进行检查,当前时间减去为启动连接而输入的时间是否超过设定时间,请使用connection.close()关闭连接!