2

我有一个使用浏览器字段连接到网页的应用程序。一切正常,模拟器显示正确的页面。

如果我将模拟器网络属性设置为“超出范围”并单击网页中的链接,那么我会得到一个异常 - 在 BrowserFieldConnectionManagerImpl

如何捕获此异常以便我可以采取适当的措施?

该应用程序正在使用 BlackBerry SDK

代码在这里:

公共最终类 example_Screen 扩展 MainScreen {

// Create the ErrorHandler class
public class MyBrowserFieldErrorHandler extends BrowserFieldErrorHandler {
    protected MyBrowserFieldErrorHandler(BrowserField browserField){
        super(browserField);
    }

    public void displayContentError(String url, String errorMessage) {
        System.out.println("JERRY: displayContentError" + url);
        System.out.println("JERRY: displayContentError" + errorMessage);
    }

    public void displayContentError(String url, InputConnection connection, Throwable t) {
        displayContentError(url, t.getMessage());
    }

    public void navigationRequestError(BrowserFieldRequest request, Throwable t) {
        displayContentError(request.getURL(), t.getMessage());
    }

    public void requestContentError(BrowserFieldRequest request, Throwable t) {
        displayContentError(request.getURL(), t.getMessage());
    }

    public InputConnection resourceRequestError(BrowserFieldRequest request, Throwable t) {
        displayContentError(request.getURL(), t.getMessage());
        InputConnection connection = null;
        return connection;
    }   

}


/**
 * Creates a new example_Screen object
 */
public example_Screen() {

    GIFEncodedImage ourAnimation = (GIFEncodedImage) GIFEncodedImage.getEncodedImageResource("2.gif");       

    AnimatedGIFField _ourAnimation = new AnimatedGIFField(ourAnimation, Field.FIELD_HCENTER + Field.FIELD_VCENTER);
    this.add(_ourAnimation);
    LabelField _ourLabelField = new LabelField("Updating ...", Field.FIELD_HCENTER + Field.FIELD_VCENTER);
    this.add(_ourLabelField);                   

    int anim_ht = _ourAnimation.getPreferredHeight();
    int label_ht = _ourLabelField.getPreferredHeight();

    EncodedImage ei = EncodedImage.getEncodedImageResource("img/menu.png");       

    int currentWidthFixed32 = Fixed32.toFP(ei.getWidth());
    int currentHeightFixed32 = Fixed32.toFP(ei.getHeight());

    int displayWidthFixed32 = Fixed32.toFP(Display.getWidth());
    int displayHeightFixed32 = Fixed32.toFP((Display.getHeight() - anim_ht - label_ht));

    int scaleXFixed32 = Fixed32.div(currentWidthFixed32, displayWidthFixed32);
    int scaleYFixed32 = Fixed32.div(currentHeightFixed32, displayHeightFixed32);

    ei = ei.scaleImage32(scaleXFixed32, scaleYFixed32);

    BitmapField bmp = new BitmapField(ei.getBitmap(), Field.FIELD_HCENTER + Field.FIELD_VCENTER);
    add(bmp);      

    BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
    myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE, BrowserFieldConfig.NAVIGATION_MODE_POINTER);       
    BrowserField browserField = new BrowserField(myBrowserFieldConfig);
    add(browserField);

    browserField.requestContent("http://www.bbc.co.uk");

    BrowserFieldListener listener = new BrowserFieldListener() {
        public void documentAborted(BrowserField browserField, Document document) {
            System.out.println("JERRY: documentAborted");
        }

        public void documentCreated(BrowserField browserField, ScriptEngine scriptEngine, Document document) {
            System.out.println("JERRY: documentCreated");
        }

        public void documentError(BrowserField browserField, Document document) {
            System.out.println("JERRY: documentError");
        }

        public void documentLoaded(BrowserField browserField, Document document) {
            System.out.println("JERRY: documentLoaded");  

            Node node = document.getFirstChild();
            String nodeText = node.getTextContent();            
            int index = -1;
            if (nodeText != null) {             
                String errorText = "Error requesting content for:";                 
                index = nodeText.indexOf(errorText); 
            }

            Screen screen = browserField.getScreen();   
            try {
                synchronized (Application.getEventLock()) {
                    if (index == -1) {
                        System.out.println("JERRY: documentLoaded: no error");
                        int count = screen.getFieldCount();
                        if (count > 1) {
                            screen.deleteRange(0, (count-1));  
                            System.out.println("JERRY: documentLoaded: " + (count-1) + " fields deleted");
                        } else {
                            System.out.println("JERRY: documentLoaded: only 1 field so none deleted");
                        }
                    } else {
                        System.out.println("JERRY: documentLoaded: error");
                    }
                }
            } catch (final Exception ex) {
                System.out.println("example_Screen: documentLoaded: exception caught: " + ex.toString());
            }               

        }

        public void documentUnloading(BrowserField browserField, Document document) {
            System.out.println("JERRY: documentUnloading");
        }

        public void downloadProgress(BrowserField browserField, ContentReadEvent event) {
            System.out.println("JERRY: downloadProgress");
        }

    };
    browserField.addListener(listener);

    // Attach the Error Handler to the BrowserField
    BrowserFieldErrorHandler eHandler = new MyBrowserFieldErrorHandler(browserField);
    browserField.getConfig().setProperty(BrowserFieldConfig.ERROR_HANDLER, eHandler);

}  

}

4

1 回答 1

2

BrowserField包含一个方法addListener(),它引用BrowserFieldListener实现。

扩展BrowserFieldListener和处理方法documentError()documentAborted()此实现中的错误。

然后添加您的类实例的引用,该引用BrowserFieldListener通过 延伸到您的浏览器字段browserField.addListener(browserFieldListener);

编辑:

如果这不起作用,则使用RIM API 中的BrowserFieldErrorHandler类。构建您自己的错误处理程序并将其实例传递给浏览器字段配置。

下面是示例代码:

// Create the ErrorHandler class
public class MyBrowserFieldErrorHandler extends BrowserFieldErrorHandler {
  public void displayContentError(String url, String errorMessage) {
    String error = "Error: (url=" + url + "): " + t.getMessage();
    Dialog.ask(Dialog.D_OK, error);
    logMessage(“BrowserFieldError: “ + error );
  }

 public void displayContentError(String url, InputConnection connection, Throwable t) { 
    displayContentError(url, t.getMessage());
 }

 public void requestContentError(BrowserFieldRequest request, Throwable t){
  displayContentError(request.getURL(), t.getMessage());
 }
}

// Attach the Error Handler to the BrowserField
BrowserFieldErrorHandler eHandler = new MyBrowserFieldErrorHandler();
browserField.getConfig().setProperty(BrowserFieldConfig.ERROR_HANDLER,eHandler);

我从浏览器字段功能的 DevCon2010 演示中获得了这个示例代码。你可以在这里得到它:http: //dev.tuyennguyen.ca/wp-content/uploads/2011/02/DEV49.pdf

于 2012-09-07T16:16:40.320 回答