0

我是 BB 编程新手。尝试在应用程序中打开 google url。我在测试时

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

RenderingSession#getBrowserContent() 抛出 RenderingException

RenderingSession#getBrowserContent() 抛出 net.rim.device.api.browser.field.RenderingException: IOException in connection

我启用了 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);        
    }
}
4

1 回答 1

3

这是我会尝试的:

  1. 首先,获取您尝试加载的 URL(看起来是http://www.google.com),退出您的应用程序,然后使用模拟器的普通浏览器应用程序访问相同的 URL。它有效吗?如果在浏览器里不行,那在你的app里也行不通,需要调试一下模拟器连接不上网络的原因。

  2. 您是否有阻止模拟器的软件防火墙(例如 Windows 防火墙)?如果您确实运行了防火墙,则需要确保fledge.exe允许该进程建立网络连接,否则这将不起作用。您可能还需要为该java.exe过程授予例外。对于 BB 7.1 插件,fledge 过程对我来说是这样的:

    C:\eclipse\indigo\plugins\net.rim.ejde.componentpack7.1.0_7.1.0.10\components\simulator\fledge.exe

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

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

  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 示例文件夹中找到一个单独的演示。它被称为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 控制台窗口中查找堆栈跟踪。这应该提供更多信息。

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

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

在此处输入图像描述

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

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

于 2013-02-03T10:30:35.677 回答