Problem executing browserfield with “;deviceside=true” on BlackBerry sim

倖福魔咒の 提交于 2019-12-02 11:19:22

问题


I am trying to use the sample code given in the Apress Advanced BlackBerry Development book. The problem is that when I launch the simulator with MDS, the web page gets displayed. But when I append ";deviceside=true" to the url, and then launch the simulator without MDS, the web page is not displayed. Only a white Screen is displayed.

import java.io.IOException;
import java.util.*;

import javax.microedition.io.*;

import net.rim.device.api.browser.field.*;
import net.rim.device.api.io.http.HttpHeaders;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.*;

public class BrowserScreen extends MainScreen implements Runnable,
        RenderingApplication
{
    private RenderingSession renderSession;
    private LabelField status;
    private StatusUpdater updater;
    private String url;

    public BrowserScreen()
    {
        renderSession = RenderingSession.getNewInstance();
        status = new LabelField("Loading...");
        add(status);
        updater = new StatusUpdater(status);
        url = "http://www.google.com";
        (new Thread(this)).start();
    }

    private class BrowserFieldContainer extends VerticalFieldManager
    {
        public BrowserFieldContainer()
        {
            super(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR
                    | Manager.FIELD_HCENTER);
        }

        public void sublayout(int maxWidth, int maxHeight)
        {
            int width = BrowserScreen.this.getWidth();
            int height = BrowserScreen.this.getHeight();
            super.sublayout((int) (width * .9), height / 2);
        }
    }

    public void run()
    {
        HttpConnection conn = null;
        try
        {
            conn = (HttpConnection) Connector.open(url);
            updater.sendDelayedMessage("Connection opened");
            BrowserContent browserContent = renderSession.getBrowserContent(
                    conn, this, null);
            if (browserContent != null)
            {
                Field field = browserContent.getDisplayableContent();
                if (field != null)
                {
                    synchronized (Application.getEventLock())
                    {
                        deleteAll();
                        add(status);
                        add(new LabelField("Your search starts here."));
                        BrowserFieldContainer container = 
                            new BrowserFieldContainer();
                        container.add(field);
                        add(container);
                        add(new LabelField("Don't forget to tip the service!"));
                    }
                }
                browserContent.finishLoading();
            }
        }
        catch (Exception e)
        {
            updater.sendDelayedMessage(e.getMessage());
        }
        finally
        {
            try
            {
                if (conn != null)
                {
                    conn.close();
                }
            }
            catch (Exception e)
            {
            }
        }
    }

    public Object eventOccurred(Event event)
    {
        if (event.getUID() == Event.EVENT_URL_REQUESTED)
        {
            UrlRequestedEvent urlRequestedEvent = (UrlRequestedEvent) event;
            url = urlRequestedEvent.getURL();
            (new Thread(this)).start();
        }
        updater.sendDelayedMessage("Handle event " + event.getUID() + " for "
                + event.getSourceURL());
        return null;
    }

    public int getAvailableHeight(BrowserContent browserContent)
    {
        return getHeight() / 2;
    }

    public int getAvailableWidth(BrowserContent browserContent)
    {
        return (int) (getWidth() * .9);
    }

    public String getHTTPCookie(String url)
    {
        return null;
    }

    public int getHistoryPosition(BrowserContent browserContent)
    {
        return 0;
    }

protected HttpConnection getResourceConnection(String url,
        HttpHeaders requestHeaders)
{
    HttpConnection connection = null;
    try
    {
        connection = (HttpConnection) Connector.open(url);
        if (requestHeaders != null)
        {
            Hashtable headers = requestHeaders.toHashtable();
            if (headers != null)
            {
                Enumeration names = headers.keys();
                while (names.hasMoreElements())
                {
                    String name = (String) names.nextElement();
                    String value = (String) headers.get(name);
                    connection.setRequestProperty(name, value);
                }
            }
        }
    }
    catch (IOException ioe)
    {
        updater.sendDelayedMessage(ioe.getMessage());
    }
    return connection;
}

    public HttpConnection getResource(final RequestedResource resource,
            final BrowserContent referrer)
    {
        if (resource == null || resource.isCacheOnly())
        {
            return null;
        }

        String url = resource.getUrl();

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

        if (referrer == null)
        {
            return getResourceConnection(resource.getUrl(), resource
                    .getRequestHeaders());
        }
        else
        {
            (new Thread()
            {
                public void run()
                {
                    HttpConnection connection = getResourceConnection(resource
                            .getUrl(), resource.getRequestHeaders());
                    resource.setHttpConnection(connection);
                    referrer.resourceReady(resource);
                }
            }).start();
        }
        return null;
    }

    public void invokeRunnable(Runnable runnable)
    {
        (new Thread(runnable)).start();
    }

}

回答1:


check your argument to deviceSide; you type "ture" instead of "true"!



来源:https://stackoverflow.com/questions/4736119/problem-executing-browserfield-with-deviceside-true-on-blackberry-sim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!