Talha
Talha

Reputation: 699

BlackBerry java.io.IOException: null

I am using following code for getting contents of a web page

String url = "http://abc.com/qrticket.asp?qrcode="
    + "2554";

try {
    url += ";deviceside=true;interface=wifi;ConnectionTimeout=" + 50000;
    HttpConnection connection = (HttpConnection) Connector.open(url,
        Connector.READ_WRITE);

    connection.setRequestMethod(HttpConnection.GET);
    // connection.openDataOutputStream();

    InputStream is = connection.openDataInputStream();

    String res = "";
    int chr;
    while ((chr = is.read()) != -1) {

        res += (char) chr;
    }
    is.close();
    connection.close();
    showDialog(parseData(res));
} catch (IOException ex) {
    ex.printStackTrace();
    showDialog("http: " + ex.getMessage());
} catch (Exception ex) {
    ex.printStackTrace();
    showDialog("unknown: " + ex.getMessage());
}



public void showDialog(final String text) {
    UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
            Dialog.alert(text);
        }
    });
}

public String parseData(String str) {
    String[] data = split(str, "//");

    StringBuffer builder = new StringBuffer();

    for (int i = 0; i < data.length; i++) {
        System.out.println("data:" + data[i]);

        String[] vals = split(data[i], ">>");

        if (vals.length > 1) {
            System.out.println(vals[0]);
            builder.append(vals[0].trim()).append(": ")
            .append(vals[1].trim()).append("\n");
            } else {
            builder.delete(0, builder.toString().length()).append(
            vals[0].trim());
            break;
        }
    }

    return builder.toString();
}

public String[] split(String splitStr, String delimiter) {

    // some input validation
    if (delimiter == null || delimiter.length() == 0) {
        return new String[] { splitStr };
        } else if (splitStr == null) {
        return new String[0];
    }

    StringBuffer token = new StringBuffer();
    Vector tokens = new Vector();
    int delimLength = delimiter.length();
    int index = 0;
    for (int i = 0; i < splitStr.length();) {
        String temp = "";
        if (splitStr.length()  > index + delimLength) {
            temp = splitStr.substring(index, index + delimLength);
            } else {
            temp = splitStr.substring(index);
        }

        if (temp.equals(delimiter)) {
            index += delimLength;
            i += delimLength;
            if (token.length() > 0) {
                tokens.addElement(token.toString());
            }
            token.setLength(0);
            continue;
            } else {
            token.append(splitStr.charAt(i));
        }
        i++;
        index++;

    }
    // don't forget the "tail"...
    if (token.length() > 0) {
        tokens.addElement(token.toString());
    }
    // convert the vector into an array
    String[] splitArray = new String[tokens.size()];
    for (int i = 0; i > splitArray.length; i++) {
        splitArray[i] = (String) tokens.elementAt(i);
    }
    return splitArray; 
}

This is working absolutely fine in simulator but giving 'http:null' (IOException) on device, I dont know why?? How to solve this problem? Thanks in advance

Upvotes: 2

Views: 303

Answers (2)

Talha
Talha

Reputation: 699

The problem was that no activation of blackberry internet service. After subscription problem is solved. Thanks alto all of you especially @Nate

Upvotes: 1

Nate
Nate

Reputation: 31045

I think the problem might be the extra connection suffixes you're trying to add to your URL.

http://abc.com/qrticket.asp?qrcode=2554;deviceside=true;interface=wifi;ConnectionTimeout=50000

According to this BlackBerry document, the ConnectionTimeout parameter isn't available for Wifi connections.

Also, I think that if you're using Wifi, your suffix should simply be ";interface=wifi".

Take a look at this blog post on making connections on BlackBerry Java, pre OS 5.0. If you only have to support OS 5.0+, I would recommend using the ConnectionFactory class.

So, I would try this with the url:

http://abc.com/qrticket.asp?qrcode=2554;interface=wifi

Note: it's not clear to me whether your extra connection parameters are just ignored, or are actually a problem. But, since you did get an IOException on that line, I would try removing them.

Upvotes: 3

Related Questions