1

我正在使用以下代码来获取网页的内容

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),我不知道为什么?如何解决这个问题呢?提前致谢

4

2 回答 2

2

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

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

根据此 BlackBerry 文档,该ConnectionTimeout参数不适用于 Wifi 连接。

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

查看这篇关于在 OS 5.0 之前的 BlackBerry Java 上建立连接的博文。如果您只需要支持 OS 5.0+,我建议您使用ConnectionFactory 类

所以,我会用 url 试试这个:

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

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

于 2013-06-03T09:06:36.533 回答
1

问题是没有激活黑莓互联网服务。订阅问题解决后。谢谢大家,尤其是@Nate

于 2013-06-04T07:21:12.527 回答