0
votes

Application is not responding while consuming restful service if internet is connected but browsing is not working, in blackberry jre 5.0. Please guide me if there is any method to set the responce time out?

I am using this code to consume service.

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
Yes i did but facing same problem. - Farhan Ali
you can access the internet on your browser ? run the url with your browser - Signare
no i just check that internet is working on browser or not - Farhan Ali
are you making a network connection on the event thread? never do it. - rfsk2010

1 Answers

0
votes

use it

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

Here timeout is given in milliseconds. Once timeout will happen , it can be handle in catch block.