BrowserField和丢失的连接

时间:2012-09-07 14:23:01

标签: blackberry browserfield

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

如果我将模拟器网络属性设置为“Out of Coverage”并单击我的网页中的链接,那么我得到一个例外 - 在BrowserFieldConnectionManagerImpl

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

该应用正在使用BlackBerry SDK

代码在这里:

public final class 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);

}  

}

1 个答案:

答案 0 :(得分:2)

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

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

然后通过BrowserFieldListener添加将browserField.addListener(browserFieldListener);扩展到您的浏览器字段的类实例的引用。

修改

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

下面是示例代码:

// 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的BrowserField功能演示中获取此示例代码。你可以在这里得到它:http://dev.tuyennguyen.ca/wp-content/uploads/2011/02/DEV49.pdf