使用browserfield时,黑莓模拟器无法加载

时间:2013-01-22 05:53:28

标签: blackberry blackberry-simulator blackberry-eclipse-plugin blackberry-jde blackberry-webworks

我是BB编程的新手。在应用程序内部打开谷歌网址。我在测试中

模拟器9900,它显示如下错误:

RenderingSession#getBrowserContent() threw RenderingException

  

RenderingSession#getBrowserContent()抛出net.rim.device.api.browser.field.RenderingException:连接中的IOException

我启用了MDS-CS。但我仍然遇到了这个问题。

代码如下,即BB给出的样本。

public final class BrowserFieldDemo extends UiApplication implements RenderingApplication 
{

    private static final String REFERER = "referer";  

    private RenderingSession _renderingSession;   
    private HttpConnection  _currentConnection;
    private MainScreen _mainScreen;


    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args) 
    {
        BrowserFieldDemo app = new BrowserFieldDemo();

        // Make the currently running thread the application's event
        // dispatch thread and begin processing events.
        app.enterEventDispatcher();
    }

    /**
     * Creates a new BrowserFieldDemo object
     */
    public BrowserFieldDemo() 
    {       
        _mainScreen = new MainScreen(Screen.HORIZONTAL_SCROLL);        
        pushScreen(_mainScreen);
        _renderingSession = RenderingSession.getNewInstance();

        // Enable javascript
        //_renderingSession.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.JAVASCRIPT_ENABLED, true);                        

        PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread("http://www.google.com", null, null, null, this);
        thread.start();            
    }

    /**
     * Processes an http connection
     * 
     * @param connection The connection to the web content
     * @param e The event triggering the connection
     */
    void processConnection(HttpConnection connection, Event e) 
    {
        // Cancel previous request
        if (_currentConnection != null) 
        {
            try 
            {
                _currentConnection.close();
            } 
            catch (IOException e1) 
            {                
            }
        }

        _currentConnection = connection;

        BrowserContent browserContent = null;

        try 
        {
            browserContent = _renderingSession.getBrowserContent(connection, this, e);

            if (browserContent != null) 
            {
                Field field = browserContent.getDisplayableContent();

                if (field != null) 
                {
                    synchronized (Application.getEventLock()) 
                    {
                        _mainScreen.deleteAll();
                        _mainScreen.add(field);
                    }
                }

                browserContent.finishLoading();
            }

        } 
        catch (RenderingException re) 
        {
            Utilities.errorDialog("RenderingSession#getBrowserContent() threw " + re.toString());
        } 
        finally 
        {
            SecondaryResourceFetchThread.doneAddingImages();
        }

    }    

    /**
     * @see net.rim.device.api.browser.field.RenderingApplication#eventOccurred(Event)
     */
    public Object eventOccurred(Event event) 
    {
        int eventId = event.getUID();

        switch (eventId) 
        {
            case Event.EVENT_URL_REQUESTED : 
            {
                UrlRequestedEvent urlRequestedEvent = (UrlRequestedEvent) event;    

                PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread(urlRequestedEvent.getURL(),
                                                                                         urlRequestedEvent.getHeaders(), 
                                                                                         urlRequestedEvent.getPostData(),
                                                                                         event, this);
                thread.start();

                break;

            } 
            case Event.EVENT_BROWSER_CONTENT_CHANGED: 
            {                
                // Browser field title might have changed update title.
                BrowserContentChangedEvent browserContentChangedEvent = (BrowserContentChangedEvent) event; 

                if (browserContentChangedEvent.getSource() instanceof BrowserContent) 
                { 
                    BrowserContent browserField = (BrowserContent) browserContentChangedEvent.getSource(); 
                    String newTitle = browserField.getTitle();
                    if (newTitle != null) 
                    {
                        synchronized (getAppEventLock()) 
                        { 
                            _mainScreen.setTitle(newTitle);
                        }                                               
                    }                                       
                }                   

                break;                

            } 
            case Event.EVENT_REDIRECT : 
            {
                RedirectEvent e = (RedirectEvent) event;
                String referrer = e.getSourceURL();

                switch (e.getType()) 
                {  
                    case RedirectEvent.TYPE_SINGLE_FRAME_REDIRECT :
                        // Show redirect message.
                        Application.getApplication().invokeAndWait(new Runnable() 
                        {
                            public void run() 
                            {
                                Status.show("You are being redirected to a different page...");
                            }
                        });

                    break;

                    case RedirectEvent.TYPE_JAVASCRIPT :
                        break;

                    case RedirectEvent.TYPE_META :
                        // MSIE and Mozilla don't send a Referer for META Refresh.
                        referrer = null;     
                        break;

                    case RedirectEvent.TYPE_300_REDIRECT :
                        // MSIE, Mozilla, and Opera all send the original
                        // request's Referer as the Referer for the new
                        // request.
                        Object eventSource = e.getSource();
                        if (eventSource instanceof HttpConnection) 
                        {
                            referrer = ((HttpConnection)eventSource).getRequestProperty(REFERER);
                        }

                        break;
                    }

                    HttpHeaders requestHeaders = new HttpHeaders();
                    requestHeaders.setProperty(REFERER, referrer);
                    PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread(e.getLocation(), requestHeaders,null, event, this);
                    thread.start();
                    break;

            } 
            case Event.EVENT_CLOSE :
                // TODO: close the appication
                break;

            case Event.EVENT_SET_HEADER :        // No cache support.
            case Event.EVENT_SET_HTTP_COOKIE :   // No cookie support.
            case Event.EVENT_HISTORY :           // No history support.
            case Event.EVENT_EXECUTING_SCRIPT :  // No progress bar is supported.
            case Event.EVENT_FULL_WINDOW :       // No full window support.
            case Event.EVENT_STOP :              // No stop loading support.
            default :
        }

        return null;
    }

    /**
     * @see net.rim.device.api.browser.field.RenderingApplication#getAvailableHeight(BrowserContent)
     */
    public int getAvailableHeight(BrowserContent browserField) 
    {
        // Field has full screen.
        return Display.getHeight();
    }

    /**
     * @see net.rim.device.api.browser.field.RenderingApplication#getAvailableWidth(BrowserContent)
     */
    public int getAvailableWidth(BrowserContent browserField) 
    {
        // Field has full screen.
        return Display.getWidth();
    }

    /**
     * @see net.rim.device.api.browser.field.RenderingApplication#getHistoryPosition(BrowserContent)
     */
    public int getHistoryPosition(BrowserContent browserField) 
    {
        // No history support.
        return 0;
    }


    /**
     * @see net.rim.device.api.browser.field.RenderingApplication#getHTTPCookie(String)
     */
    public String getHTTPCookie(String url) 
    {
        // No cookie support.
        return null;
    }

    /**
     * @see net.rim.device.api.browser.field.RenderingApplication#getResource(RequestedResource, BrowserContent)
     */
    public HttpConnection getResource( RequestedResource resource, BrowserContent referrer) 
    {
        if (resource == null) 
        {
            return null;
        }

        // Check if this is cache-only request.
        if (resource.isCacheOnly()) 
        {
            // No cache support.
            return null;
        }

        String url = resource.getUrl();

        if (url == null) 
        {
            return null;
        }

        // If referrer is null we must return the connection.
        if (referrer == null) 
        {
            HttpConnection connection = Utilities.makeConnection(resource.getUrl(), resource.getRequestHeaders(), null);

            return connection;

        } 
        else 
        {
            // If referrer is provided we can set up the connection on a separate thread.
            SecondaryResourceFetchThread.enqueue(resource, referrer);
        }

        return null;
    }

    /**
     * @see net.rim.device.api.browser.field.RenderingApplication#invokeRunnable(Runnable)
     */
    public void invokeRunnable(Runnable runnable) 
    {       
        (new Thread(runnable)).start();
    }
}

/**
 * A Thread class to fetch content using an http connection
 */
final class PrimaryResourceFetchThread extends Thread 
{    
    private BrowserFieldDemo _application;
    private Event _event;
    private byte[] _postData;
    private HttpHeaders _requestHeaders;
    private String _url;

    /**
     * Constructor to create a PrimaryResourceFetchThread which fetches the web
     * resource from the specified url.
     * 
     * @param url The url to fetch the content from
     * @param requestHeaders The http request headers used to fetch the content
     * @param postData Data which is to be posted to the url
     * @param event The event triggering the connection
     * @param application The application requesting the connection
     */
    PrimaryResourceFetchThread(String url, HttpHeaders requestHeaders, byte[] postData, Event event, BrowserFieldDemo application) 
    {
        _url = url;
        _requestHeaders = requestHeaders;
        _postData = postData;
        _application = application;
        _event = event;
    }

    /**
     * Connects to the url associated with this object
     * 
     * @see java.lang.Thread#run()
     */
    public void run() 
    {
        HttpConnection connection = Utilities.makeConnection(_url, _requestHeaders, _postData);
        _application.processConnection(connection, _event);        
    }
}

1 个答案:

答案 0 :(得分:3)

以下是我要尝试的内容:

  1. 首先,获取您尝试加载的网址(看起来是http://www.google.com),退出您的应用,然后使用模拟器的普通浏览器应用访问相同的网址。它有用吗?如果它在浏览器中不起作用,它将无法在您的应用程序中运行,您需要调试模拟器无法连接到网络的原因。

  2. 您是否有阻止模拟器的软件防火墙(例如Windows防火墙)?如果您确实在运行防火墙,则需要确保允许fledge.exe进程建立网络连接,否则这将无效。您还可能需要为java.exe进程授予例外。在BB 7.1插件的情况下,承诺过程就在我身边:

    C:\蚀\靛蓝\插件\ net.rim.ejde.componentpack7.1.0_7.1.0.10 \部件\模拟器\ fledge.exe

    当然,该路径的根目录会有所不同,具体取决于您安装Eclipse的位置。

  3. 在Java代码中尝试更改网址。所有网址都会出现同样的问题吗?

  4. http://www.google.com由于某种原因已被屏蔽您所在的位置?我不知道互联网服务提供商如何在世界各地工作。至少,您可能需要使用http://www.google.co.in或类似的东西。

  5. 我不确定您尝试定位的是哪个版本的BB OS。现在大多数人(如果他们仍然支持BlackBerry Java)不支持低于OS 5.0的任何东西。如果您可以限制对OS 5.0+的支持,那么我建议不要使用该演示中的代码。在5.0中有BrowserField类,也称为 BrowserField2 。这个类更容易使用。您可以在插件的同一个BlackBerry samples文件夹中找到相应的演示。它被称为browserfield2demo。尝试运行该演示,看看是否有同样的问题。

  6. 如果您想坚持使用原始(5.0之前的版本)BrowserFieldDemo,那么您可以修改代码以获取更多调试信息。 例外来自此处:

    catch (RenderingException re) 
    {
        Utilities.errorDialog("RenderingSession#getBrowserContent() threw " + re.toString());
    } 
    

    尝试将代码更改为

    catch (RenderingException re) 
    {
        Utilities.errorDialog("RenderingSession#getBrowserContent() threw " + re.toString());
        System.out.println(re.printStackTrace());
    } 
    

    并在Eclipse Console窗口中查看堆栈跟踪。这应该提供更多信息。

  7. 您是否可以在关闭网络连接的情况下运行模拟器?有些人(包括我自己)习惯于在关闭网络连接的情况下启动模拟器,希望它能加速这个过程。当然,如果您正在测试的应用程序使用网络,那么您必须在启动应用程序之前通过选项启用它。如果网络连接(例如GSM或Wi-Fi)关闭,上面的步骤(1)将失败,但您可能没有注意到为什么它失败。

  8. 您的Eclipse Run / Debug配置设置是否在模拟器启动时自动运行应用程序?如果是这样,我建议不要这样做,并将其配置为让您手动运行应用程序,方法是单击应用程序图标。您可以在调试配置窗口中查看是否这样设置,模拟器 - > 常规窗格。清除启动时启动应用或网址

  9. enter image description here

    这些是我的建议。它看起来像一般的网络故障。我的猜测是防火墙问题(请记住,您可能必须在安装的每个JDE插件版本中为模拟器添加防火墙例外... 5.0,6.0,7.0,7.1)。

    但请尝试这些步骤并发布更多信息。祝你好运!