在Blackberry中使用Web服务时未捕获异常

时间:2012-08-13 05:57:06

标签: web-services rest blackberry blackberry-simulator blackberry-eclipse-plugin

在黑莓jre 5.0中,如果连接互联网但浏览无效,应用程序在使用休息服务时没有响应。如果有任何方法可以设置响应时间,请指导我吗?

我正在使用此代码来使用服务。

static HttpConnection con = null;
static InputStream is = null;
static StringBuffer rawResponse;

public static String Call(String url) throws IOException {
    try {
        url = url + getConnectionString() + ";";
        if (CheckConn()) {
            con = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
            is = con.openInputStream();
            byte[] responseData = new byte[10000];
            int length = 0;
            rawResponse = new StringBuffer();
            while (-1 != (length = is.read(responseData))) {
                rawResponse.append(new String(responseData, 0, length));
            }
        } else {
            Status.show("Internet service is not avaiable.");
        }
    } catch (IOException ex) {
        Status.show("Internet is not responding");
    } finally {
        try {
            is.close();
            con.close();
        } catch (Exception e) {
            Status.show(e.getMessage());
        }
    }
    return rawResponse.toString();
}

public static String getConnectionString() {
    String connectionString = "";
    if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
        connectionString = ";interface=wifi";
    }
    else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
        connectionString = ";deviceside=false";
    } 
    else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
        String carrierUid = getCarrierBIBSUid();
        if (carrierUid == null) {
            connectionString = ";deviceside=true";
        } else {
            connectionString = ";deviceside=false;connectionUID=" + carrierUid + ";ConnectionType=mds-public";
        }
    } 
    else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {
        connectionString = ";None";
    }
    return connectionString;
}


public static String getCarrierBIBSUid() {
    net.rim.device.api.servicebook.ServiceRecord[] records = ServiceBook.getSB().getRecords();
    int currentRecord;
    for (currentRecord = 0; currentRecord < records.length; currentRecord++) {
        if (records[currentRecord].getCid().toLowerCase().equals("ippp")) {
            if (records[currentRecord].getName().toLowerCase().indexOf("bibs") >= 0) {
                return records[currentRecord].getUid();
            }
        }
    }
    return null;
}

1 个答案:

答案 0 :(得分:0)

使用它

 url = url + getConnectionString() + ";ConnectionTimeout=25000";

此处超时以毫秒为单位。一旦发生超时,它就可以在catch块中处理。