Login Screen
Settings Screen
import net.rim.device.api.io.URI;
import net.rim.device.api.io.messaging.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.system.Application;
import java.io.*;
public class NetworkSample extends UiApplication
{
public static void main(String[] args)
{
NetworkSample app = new NetworkSample();
app.enterEventDispatcher();
}
public NetworkSample()
{
pushScreen(new NonBlockingSenderSample());
}
}
class NonBlockingSenderSample extends MainScreen
implements FieldChangeListener
{
ButtonField _btnNonBlock = new ButtonField(Field.FIELD_HCENTER);
private static UiApplication _app = UiApplication.getUiApplication();
public NonBlockingSenderSample()
{
_btnNonBlock.setChangeListener(this);
_btnNonBlock.setLabel("Fetch page");
add(_btnNonBlock);
}
public void fieldChanged(Field button, int unused)
{
if(button == _btnNonBlock)
{
NonBlockingSenderDestination destination = null;
try
{
//URI uri = URI.create("http://www.blackberry.com");
URI uri = URI.create("https://auth.services.mozilla.com/user/1.0/nprm3itjzzndm2bh2tmtmzbanfkw4mwh/node/weave");
NBSDMsgListener responseListener = new NBSDMsgListener();
destination = (NonBlockingSenderDestination)
DestinationFactory.getSenderDestination
("CommAPISample", uri);
if (destination == null)
{
destination =
DestinationFactory.createNonBlockingSenderDestination
(new Context("CommAPISample"), uri, responseListener);
}
// Send message to retrieve the response
destination.send();
}
catch(Exception e)
{
// process the error
}
}
}
}
class NBSDMsgListener implements MessageListener
{
public void onMessage(Destination dest, Message msg)
{
String payload = null;
if (msg instanceof ByteMessage)
{
ByteMessage reply = (ByteMessage) msg;
payload = (String) reply.getStringPayload();
} else if(msg instanceof StreamMessage)
{
StreamMessage reply = (StreamMessage) msg;
InputStream is = reply.getStreamPayload();
byte[] data = null;
try {
data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
} catch (IOException e) {
}
if(data != null)
{
payload = new String(data);
}
}
if(payload!=null)
{
synchronized(Application.getEventLock())
{
UiApplication.getUiApplication().pushScreen
(new HTTPOutputScreen(payload));
}
}
}
public void onMessageCancelled(Destination arg0, int arg1)
{
// process message cancelled notification
}
public void onMessageFailed(Destination arg0, MessageFailureException arg1)
{
// process message failed notification
}
}
class HTTPOutputScreen extends MainScreen
{
RichTextField _rtfOutput = new RichTextField();
public HTTPOutputScreen(String message)
{
_rtfOutput.setText("Retrieving data. Please wait...");
add(_rtfOutput);
showContents(message);
}
// After the data has been retrieved, display it
public void showContents(final String result) {
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
_rtfOutput.setText(result);
}
});
}
}import net.rim.device.api.io.transport.*;
import net.rim.device.api.io.transport.options.*;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.util.Arrays;
import java.io.*;
import javax.microedition.io.*;
public class NetworkSample extends UiApplication
{
public static void main(String[] args)
{
NetworkSample app = new NetworkSample();
app.enterEventDispatcher();
}
public NetworkSample()
{
new HTTPConnectionSetup();
}
}
class HTTPConnectionSetup
{
ConnectionFactory _factory = new ConnectionFactory();
public HTTPConnectionSetup()
{/*****************************************************************************
Comment out: creating network transport list
*****************************************************************************
// Create preference ordered list of transports
int[] _intTransports =
{ TransportInfo.TRANSPORT_TCP_WIFI,
TransportInfo.TRANSPORT_WAP2,
TransportInfo.TRANSPORT_TCP_CELLULAR
};
// Remove any transports that are not (currently) available
for(int i = 0; i < _intTransports.length ; i++)
{
int transport = _intTransports[i];
if(!TransportInfo.isTransportTypeAvailable(transport)
|| !TransportInfo.hasSufficientCoverage(transport))
{
Arrays.removeAt(_intTransports, i);
}
}
// Set options for TCP Cellular transport
TcpCellularOptions tcpOptions = new TcpCellularOptions();
if(!TcpCellularOptions.isDefaultAPNSet())
{
tcpOptions.setApn("My APN");
tcpOptions.setTunnelAuthUsername("user");
tcpOptions.setTunnelAuthPassword("password");
}
// Set ConnectionFactory options
if(_intTransports.length > 0)
{
_factory.setPreferredTransportTypes(_intTransports);
}
_factory.setTransportTypeOptions(TransportInfo.TRANSPORT_TCP_CELLULAR,
tcpOptions);
_factory.setAttemptsLimit(5);
*****************************************************************************/
// Open a connection on a new thread
Thread t = new Thread(new Runnable()
{
public void run()
{
//ConnectionDescriptor cd = _factory.getConnection("http://www.blackberry.com");
ConnectionDescriptor cd = _factory.getConnection("https://auth.services.mozilla.com/user/1.0/nprm3itjzzndm2bh2tmtmzbanfkw4mwh/node/weave");
// If connection was successful, fetch and show the content from
// the web server
if(cd != null)
{
Connection c = cd.getConnection();
displayContent(c);
}
}
});
t.start();
}
private void displayContent(final Connection conn)
{
// When the connection thread completes, show the data from the web server
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
UiApplication.getUiApplication().pushScreen(new HTTPOutputScreen(conn));
}
});
}
}
class HTTPOutputScreen extends MainScreen
{
RichTextField _rtfOutput = new RichTextField();
public HTTPOutputScreen(Connection conn)
{
// Create a container for the data, and put it on the screen
_rtfOutput.setText("Retrieving data. Please wait...");
add(_rtfOutput);
// Retrieve the data from the web server, using the connection, on a
// separate thread
ContentReaderThread t = new ContentReaderThread(conn);
t.start();
}
// After the data has been retrieved, display it
public void showContents(final String result)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
_rtfOutput.setText(result);
}
});
}
private final class ContentReaderThread extends Thread
{
private Connection _connection;
ContentReaderThread(Connection conn)
{
_connection = conn;
}
public void run()
{
String result = "";
OutputStream os = null;
InputStream is = null;
try
{
// Send HTTP GET to the server
OutputConnection outputConn = (OutputConnection) _connection;
os = outputConn.openOutputStream();
String getCommand = "GET " + "/" + " HTTP/1.0\r\n\r\n";
os.write(getCommand.getBytes());
os.flush();
// Get InputConnection and read the server's response
InputConnection inputConn = (InputConnection) _connection;
is = inputConn.openInputStream();
byte[] data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
result = new String(data);
// is.close();
}
catch(Exception e)
{
result = "ERROR fetching content: " + e.toString();
}
finally
{
// Close OutputStream
if(os != null)
{
try
{
os.close();
}
catch(IOException e)
{
}
}
// Close InputStream
if(is != null)
{
try
{
is.close();
}
catch(IOException e)
{
}
}
// Close Connection
try
{
_connection.close();
}
catch(IOException ioe)
{
}
}
// Show the response received from the web server, or an error message
showContents(result);
}
}
}*
Static Utility Functions
*
public static void SaveData?(long _key, Object _data) throws Exception {
Create a persistent data store
PersistentObject? _store;
_store = PersistentStore?.getPersistentObject(_key);
Store Persistent data
synchronized(_store){
_store.setContents(_data);}
_store.commit();
}
public static Object LoadData?(long _key) throws Exception {
Create a persistent data store
PersistentObject? _store;
_store = PersistentStore?.getPersistentObject(_key);
Retrieve persistent data
Object _data;
synchronized(_store) {
_data = (Object)_store.getContents();
}}
return _data;
public String _data;}
public String _payload;
public String _collection;
public String _id;
public String _modified;
public int _ttl;
public int _sortIndex;
public AESKey _key;
public InitializationVector? _iv;
Private inner classes
*
final private class SaveOB implements FieldChangeListener? {
public void fieldChanged(Field field, int context) {
try {
Record _data = new Record();
_data._data = "data 1";
_data._id = "id 1";
long _key = 0x4ee5665b495a4912L;
BerrySyncPersistentStore?.SaveData?(_key, _data);
}}_storeArea.setText("Save Object" + " | " + _data._data + " | " + _data._id);}
catch(Exception e) {
_networkArea.setText(e.toString());}
final private class LoadOB implements FieldChangeListener? {}
public void fieldChanged(Field field, int context) {
try {}
long _key = 0x4ee5665b495a4912L;}
Record _data = (Record)BerrySyncPersistentStore?.LoadData?(_key);
if (_data == null) {
_storeArea.setText("Load Object" + _data);}else{
_storeArea.setText("Load Object" + " | " + _data._data + " | " + _data._id);}
catch(Exception e) {
_networkArea.setText(e.toString());}