BlackBerry java.io.IOException:空
我使用以下代码获取网页的内容BlackBerry java.io.IOException:空,blackberry,java-me,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.REA
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;
}
stringurl=”http://abc.com/qrticket.asp?qrcode="
+ "2554";
试一试{
url+=“deviceside=true;interface=wifi;ConnectionTimeout=“+50000;
HttpConnection=(HttpConnection)连接器.open(url,
连接器(读/写);
setRequestMethod(HttpConnection.GET);
//connection.openDataOutputStream();
InputStream is=connection.openDataInputStream();
字符串res=“”;
int-chr;
而((chr=is.read())!=-1){
res+=(char)chr;
}
is.close();
connection.close();
显示对话框(解析数据(res));
}捕获(IOEX异常){
例如printStackTrace();
showDialog(“http:+ex.getMessage());
}捕获(例外情况除外){
例如printStackTrace();
showDialog(“未知:+ex.getMessage());
}
public void showDialog(最终字符串文本){
UiApplication.getUiApplication().invokeLater(新的Runnable()){
公开募捐{
对话框。警报(文本);
}
});
}
公共字符串解析数据(字符串str){
String[]data=split(str,“/”);
StringBuffer builder=新的StringBuffer();
对于(int i=0;i>”;
如果(vals.length>1){
System.out.println(vals[0]);
builder.append(vals[0].trim()).append(“:”)
.append(vals[1].trim()).append(“\n”);
}否则{
delete(0,builder.toString().length()).append(
VAL[0].trim());
打破
}
}
返回builder.toString();
}
公共字符串[]拆分(字符串拆分字符串,字符串分隔符){
//一些输入验证
if(delimiter==null | | delimiter.length()==0){
返回新字符串[]{splitStr};
}else if(splitStr==null){
返回新字符串[0];
}
StringBuffer令牌=新的StringBuffer();
向量标记=新向量();
int delimLength=分隔符.length();
int指数=0;
对于(int i=0;iindex+delimLength){
温度=拆分字符串(索引,索引+删除长度);
}否则{
temp=拆分子字符串(索引);
}
if(温度等于(分隔符)){
指数+=最小长度;
i+=最小长度;
if(token.length()>0){
tokens.addElement(token.toString());
}
token.setLength(0);
继续;
}否则{
token.append(splitStr.charAt(i));
}
i++;
索引++;
}
//别忘了“尾巴”。。。
if(token.length()>0){
tokens.addElement(token.toString());
}
//将向量转换为数组
String[]splitArray=新字符串[tokens.size()];
对于(int i=0;i>splitArray.length;i++){
splitArray[i]=(字符串)令牌;
}
返回拆分数组;
}
这在模拟器中运行非常好,但在设备上给出了“http:null”(IOException),我不知道为什么??
如何解决这个问题?
提前感谢我想问题可能是您试图添加到URL的额外连接后缀 根据,Wifi连接无法使用
ConnectionTimeout
参数
此外,我认为如果您使用Wifi,您的后缀应该是“interface=Wifi”
看看这个。如果您只需要支持OS5.0+,我建议您使用
因此,我会尝试使用以下url:
注意:我不清楚您的额外连接参数是被忽略了,还是实际上是个问题。但是,既然你在那条线上得到了一个
IOException
,我会尝试删除它们。问题是没有激活黑莓互联网服务。订阅问题解决后。
谢谢大家,尤其是@Nate您是否会显示函数showDialog(parseData(res));更新了问题,但是这些方法没有问题,我已经检查过了,没有解析data@Signareyes html页面,没有html标记,纯文本。当我运行代码时,我得到如下结果-我给出的url是一个伪url。HttpConnection=(HttpConnection)Connector.open(url,Connector.READ\u WRITE)上存在异常;这条线。但在移除SUFFIX后,这在模拟器上运行良好;interface=wifi我在应用程序启动@Nate时遇到错误“尝试访问安全API”。我认为这是权限错误,但我部署了已签名的应用程序。这是一个完全不同的问题
尝试访问安全API
仅表示您正在调用需要代码签名的API,但尚未对应用程序进行正确的代码签名。您需要再次检查签名密钥。但是,这与您的IOException
无关。