无法以编程方式检索网页

时间:2011-10-14 12:11:43

标签: blackberry webpage

我写了一个简单的程序,检索谷歌主页。代码如下:

HttpConnection conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.GET);
int responseCode = conn.getResponseCode();
if(responseCode == HttpConnection.HTTP_OK){
    InputStream data = conn.openInputStream();
    StringBuffer raw = new StringBuffer();
    byte[] buf = new byte[4096];
    int nRead = data.read(buf);
    while(nRead > 0){
        raw.append(new String(buf, 0, nRead));
        nRead = data.read(buf);
    }
    dest.updateDestination(raw.toString());

}
else
    dest.updateDestination("responseCode="+ Integer.toString(responseCode));            
    }
    catch(IOException e){
        e.printStackTrace();
        dest.updateDestination("Exception:"+e.toString());
    }

HttpConnection conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE); conn.setRequestMethod(HttpConnection.GET); int responseCode = conn.getResponseCode(); if(responseCode == HttpConnection.HTTP_OK){ InputStream data = conn.openInputStream(); StringBuffer raw = new StringBuffer(); byte[] buf = new byte[4096]; int nRead = data.read(buf); while(nRead > 0){ raw.append(new String(buf, 0, nRead)); nRead = data.read(buf); } dest.updateDestination(raw.toString()); } else dest.updateDestination("responseCode="+ Integer.toString(responseCode)); } catch(IOException e){ e.printStackTrace(); dest.updateDestination("Exception:"+e.toString()); }

这是我的主屏幕:

MenuItem getDataAction = new MenuItem("GetData", 100, 10){
        public void run(){
            String URL = "http://www.google.com";
            ServiceRequestThread svc = new ServiceRequestThread(URL, (MyScreen)
                    UiApplication.getUiApplication().getActiveScreen());
            svc.start();
        }
    };
    addMenuItem(getDataAction);
}

public void updateDestination(final String text){
    UiApplication.getUiApplication().invokeLater(new Runnable(){
        public void run(){
            output.setText(text);
        }
    });
}

当我在模拟器上运行我的应用程序时,我得到的异常被捕获为异常:java.io.IOException:Radio is Off 我正在使用eclipse IDE。我应该更改日食或模拟器中的任何属性。为什么我得到这个例外。解决办法是什么。谢谢

1 个答案:

答案 0 :(得分:3)

在模拟器菜单中,转到模拟 - >网络属性并查看可用网络以确保勾选“In Coverage”。同样在模拟设备上,转到“管理连接”菜单,确保启用了移动网络。