BlackBerry java.io.IOException:null

时间:2013-06-03 08:34:40

标签: blackberry java-me

我使用以下代码获取网页内容

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; 
}

这在模拟器中工作得很好,但在设备上给出'http:null'(IOException),我不知道为什么? 如何解决这个问题呢? 提前致谢

2 个答案:

答案 0 :(得分:2)

我认为问题可能是您尝试添加到网址的额外连接后缀。

  

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

根据this BlackBerry documentConnectionTimeout参数不适用于Wifi连接。

此外,我认为如果你使用Wifi,你的后缀应该只是";interface=wifi"

看看这个blog post on making connections on BlackBerry Java, pre OS 5.0。如果您只需要支持OS 5.0+,我建议您使用ConnectionFactory class

所以,我会尝试使用url:

  

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

注意:我不清楚您的额外连接参数是否被忽略,或者实际上是一个问题。但是,既然你确实在该行上获得了IOException,我会尝试删除它们。

答案 1 :(得分:1)

问题在于没有激活黑莓互联网服务。订阅问题解决后。 非常感谢所有人,特别是@Nate