我是 BB 编程新手。尝试在应用程序中打开 google url。我在测试时
模拟器9900,显示错误如下:
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);
}
}