在代号1中捕获未知的主机异常

Nik*_*hil 2 codenameone

我正在使用代号一个构建应用程序

所以问题是,我需要使用应用程序访问URL.URL会带回一些我在屏幕上显示的结果.

所以我使用这些行来做到这一点:

ConnectionRequest c = new ConnectionRequest() {

                protected void readResponse(InputStream input) throws IOException {


                    ByteArrayOutputStream bs = new ByteArrayOutputStream();

                    int ch;

                    while ((ch = input.read()) != -1) {

                        bs.write(ch);
                    }
                    serverOutput = new String(bs.toByteArray());
                    bs.close();
                }
            };
            c.setUrl("My URL HERE");
            c.setPost(false);


            NetworkManager.getInstance().addToQueueAndWait(c);
Run Code Online (Sandbox Code Playgroud)

所以,现在,如果gprs处于活动状态,则此代码可以正常工作.

但是,如果GPRS处于非活动状态,则会抛出未知主机异常

为了捕获这个错误,我试过使用这样的try catch块:

try{
 NetworkManager.getInstance().addToQueueAndWait(c);
}
catch(Exception e)
{
Sys.out.pln(e.troString());
}
Run Code Online (Sandbox Code Playgroud)

但是,我仍然在应用程序中以对话框的形式出现错误.我如何捕获此错误并自行处理?

更新1:

我不确定这是一个特定问题的代号,还是与java相关的...所以请帮我解决这个问题.

Che*_*hen 7

试试这个来处理所有连接的一般错误:

NetworkManager.getInstance().addErrorListener(new ActionListener() {

     public void actionPerformed(ActionEvent evt) {
         //handle your error here consume the event
         evt.consume();
     }
});
Run Code Online (Sandbox Code Playgroud)

或覆盖:

protected void handleErrorResponseCode(int code, String message) {
}
Run Code Online (Sandbox Code Playgroud)

和:

protected void handleException(Exception err) {
}
Run Code Online (Sandbox Code Playgroud)

在您的连接请求代码中,只为一个类执行此操作.