Hello Everyone,
I am in a big trouble. I want to implement OAUTH for twitter login on blackberry devices. I am successful in getting request token from twitter but after that using the oauth_token value received with the authorize url when i am trying to establish connection and trying to use BrowserContent and RenderingSession classes to render the page using that connection.
But i am always getting an illegalstateexception on the line browserContent.finishLoading().
I want to know few things:-
1.Is it really possible to implement oauth in blackberry.
2.Is it possible to use BrowserContent on even low end devices.
3.As i am getting html data to render the twitter login page, so to render html page while creating a RenderingSession like _renderingSession = RenderingSession.getNewInstance(); should i set RenderingOptions to enable html rendering.
Please help me out...i know i am not clear enough
![Sad](http://www.blackberryforums.com/images/smilies/icon_sad.gif)
but i hope you will be able to let me know that if i am doing anything wrong by going through my code.
And i can tell u more on further communication.
Please reply.
Thanks in advance
I have used below specified code.
package com;
import java.io.IOException;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.browser.field.BrowserContent;
import net.rim.device.api.browser.field.BrowserContentCha ngedEvent;
import net.rim.device.api.browser.field.Event;
import net.rim.device.api.browser.field.RedirectEvent;
import net.rim.device.api.browser.field.RenderingApplicat ion;
import net.rim.device.api.browser.field.RenderingExceptio n;
import net.rim.device.api.browser.field.RenderingOptions;
import net.rim.device.api.browser.field.RenderingSession;
import net.rim.device.api.browser.field.RequestedResource ;
import net.rim.device.api.browser.field.UrlRequestedEvent ;
import net.rim.device.api.io.http.HttpHeaders;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Status;
/*
* BlackBerry applications that provide a user interface must extend
* UiApplication.
*/
class AppMain extends UiApplication implements RenderingApplication {
private RenderingSession _renderingSession;
private HttpConnection _currentConnection;
private DemoScreen demoScreen;
private static final String REFERER = "referer";
String mainurl;
/**
* Entry point for application.
*/
public static void main(String[] args) {
// Create a new instance of the application.
AppMain theApp = new AppMain();
// To make the application enter the event thread and start processing
// messages,
// we invoke the enterEventDispatcher() method.
theApp.enterEventDispatcher();
}
/**
* <p>
* The default constructor. Creates all of the RIM UI components and pushes
* the application's root screen onto the UI stack.
*/
private AppMain() {
demoScreen = new DemoScreen();
// Push the main screen instance onto the UI stack for rendering.
pushScreen(demoScreen);
mainurl = demoScreen.getAuthorizeUrl();
_renderingSession = RenderingSession.getNewInstance();
// _renderingSession.getRenderingOptions().setPropert y(RenderingOptions.CORE_OPTIONS_GUID,
// RenderingOptions.ENABLE_CSS, true);
// _renderingSession.getRenderingOptions().setPropert y(RenderingOptions.CORE_OPTIONS_GUID,
// RenderingOptions.CSS_MEDIA_TYPE, "screen");
_renderingSession.getRenderingOptions().setPropert y(
RenderingOptions.CORE_OPTIONS_GUID,
RenderingOptions.ENABLE_HTML, true);
_renderingSession.getRenderingOptions().setPropert y(
RenderingOptions.CORE_OPTIONS_GUID,
RenderingOptions.JAVASCRIPT_ENABLED, true);
PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread(
mainurl, null, null, null, this);
thread.start();
}
public Object eventOccurred(Event event) {
int eventId = event.getUID();
switch (eventId) {
case Event.EVENT_URL_REQUESTED: {
UrlRequestedEvent urlRequestedEvent = (UrlRequestedEvent) event;
String absoluteUrl = urlRequestedEvent.getURL();
HttpConnection conn = null;
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) {
demoScreen.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;
}
public int getAvailableHeight(BrowserContent browserContent) {
// field has full screen
return Display.getHeight();
}
public int getAvailableWidth(BrowserContent browserContent) {
// field has full screen
return Display.getWidth();
}
public String getHTTPCookie(String url) {
// TODO Auto-generated method stub
return null;
}
public int getHistoryPosition(BrowserContent browserContent) {
// TODO Auto-generated method stub
return 0;
}
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;
}
public void invokeRunnable(Runnable runnable) {
(new Thread(runnable)).run();
}
public 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(
_currentConnection, this, e);
if (browserContent != null) {
final Field field = browserContent.getDisplayableContent();
if (field != null) {
synchronized (Application.getEventLock()) {
demoScreen.deleteAll();
demoScreen.add(field);
}
}
browserContent.finishLoading();
}
} catch (RenderingException re) {
} catch (Exception ex) {
System.out.println("exception" + ex.toString());
} finally {
SecondaryResourceFetchThread.doneAddingImages();
}
}
class PrimaryResourceFetchThread extends Thread {
private AppMain _application;
private Event _event;
private byte[] _postData;
private HttpHeaders _requestHeaders;
private String _url;
PrimaryResourceFetchThread(String url, HttpHeaders requestHeaders,
byte[] postData, Event event, AppMain application) {
_url = url;
_requestHeaders = requestHeaders;
_postData = postData;
_application = application;
_event = event;
}
public void run() {
HttpConnection connection = Utilities.makeConnection(_url,
_requestHeaders, _postData);
_application.processConnection(connection, _event);
}
}
}