Skip to content

Instantly share code, notes, and snippets.

@jlindenbaum
Created August 27, 2012 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlindenbaum/3490101 to your computer and use it in GitHub Desktop.
Save jlindenbaum/3490101 to your computer and use it in GitHub Desktop.
BlackBerry Connection Utility
import java.io.InputStream;
import java.util.Hashtable;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.InputConnection;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.io.http.HttpProtocolConstants;
import net.rim.device.api.io.transport.ConnectionDescriptor;
import net.rim.device.api.io.transport.ConnectionFactory;
import net.rim.device.api.io.transport.TransportDescriptor;
import net.rim.device.api.io.transport.TransportInfo;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.system.WLANInfo;
import com.idnoodle.controller.Settings;
public class HttpConnectionHelper {
private String url = null;
private ConnectionFactory connectionFactory = null;
private HttpConnection connection = null;
// keys to access response hashtable
public static final String RESULT_ETAG = "etag";
public static final String RESULT_DATA = "result";
public HttpConnectionHelper() {
}
public void setURL(String url) {
this.url = url;
}
/**
* GETs the helper's set URL and returns a hash table with RESULT_ETAG and RESULT_DATA indexes, where result is a string of the response data.
*
* Optionally an etag can be set, which may change the return hash to contain an etag, with a null result, if the server's data has not changed.
*
* @param etag
* @return
*/
public Hashtable getURLAsString(String etag) {
Hashtable responseHash = new Hashtable(2);
if (etag == null) {
etag = "";
}
responseHash.put(RESULT_ETAG, etag);
responseHash.put(RESULT_DATA, "");
System.out.println("### Getting url as string: " + this.url + " etag: " + etag);
if (this.url == null) {
throw new IllegalStateException("No URL set. Call setURL(String url) first");
}
try {
this.connectionFactory = new ConnectionFactory();
this.connectionFactory.setPreferredTransportTypes(Settings.kPreferredTransports);
this.connectionFactory.setAttemptsLimit(5);
this.connectionFactory.setConnectionTimeout(5000);
ConnectionDescriptor cd = this.connectionFactory.getConnection(this.url);
this.connection = (HttpConnection) cd.getConnection();
this.connection.setRequestMethod(HttpConnection.GET);
if (etag != null && !etag.equalsIgnoreCase("")) {
this.connection.setRequestProperty(HttpProtocolConstants.HEADER_IF_NONE_MATCH, etag);
}
int responseCode = this.connection.getResponseCode();
String responseEtag = this.connection.getHeaderField(HttpProtocolConstants.HEADER_ETAG);
System.out.println("### Response code is: " + responseCode + " ETAG: " + responseEtag);
if (responseCode == HttpConnection.HTTP_NOT_MODIFIED) {
System.out.println("### Response was cached: " + responseCode + " etag: " + etag);
this.connection.close();
return responseHash;
}
if (responseCode == HttpConnection.HTTP_OK) {
InputConnection inputConnection = (InputConnection) this.connection;
InputStream is = inputConnection.openInputStream();
byte[] data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
String httpResult = new String(data);
//System.out.println("### url result: " + httpResult);
if (responseEtag == null) {
responseHash.put(RESULT_ETAG, "");
}
else {
responseHash.put(RESULT_ETAG, responseEtag);
}
responseHash.put(RESULT_DATA, httpResult);
}
else {
// TODO Properly handle 500's
System.out.println("### Response was a failure: " + etag);
responseHash.put(RESULT_ETAG, etag);
}
} catch (Exception e) {
System.out.println("### An exception occured: " + e.getMessage());
e.printStackTrace();
}
return responseHash;
}
/**
* Grab the image from the network
* @param imageUrl URL for the image
* @return a Bitmap object for the image.
*/
public static Bitmap getURLAsImage(String imageUrl) {
System.out.println("### Getting image from url:" + imageUrl);
Bitmap bitmap = null;
if (quickCheckConnection() == false) {
return bitmap;
}
try {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setPreferredTransportTypes(Settings.kPreferredTransports);
connectionFactory.setAttemptsLimit(5);
connectionFactory.setConnectionTimeout(2000);
ConnectionDescriptor connectionDescriptor = connectionFactory.getConnection(imageUrl);
if(connectionDescriptor != null){
InputConnection connection = (InputConnection)connectionDescriptor.getConnection();
InputStream inputStream = connection.openInputStream();
byte[] data = IOUtilities.streamToBytes(inputStream);
EncodedImage encodedImage = EncodedImage.createEncodedImage(data, 0, data.length);
bitmap = encodedImage.getBitmap();
}
} catch (Exception e) {
System.out.println("### Error downloading image for dynamic button. url:" + imageUrl);
e.printStackTrace();
}
return bitmap;
}
public static String buildURLTransport(String url) {
StringBuffer returnUrl = new StringBuffer();
returnUrl.append(url);
try {
ConnectionDescriptor connDescriptor = new ConnectionFactory().getConnection(url);
if (connDescriptor != null) {
TransportDescriptor transDescriptor = connDescriptor.getTransportDescriptor();
String transUID = transDescriptor.getUid();
int transportType = transDescriptor.getTransportType();
if(transportType == TransportInfo.TRANSPORT_TCP_WIFI)
{
returnUrl.append(";deviceside=true;interface=wifi");
}
else if(transportType == TransportInfo.TRANSPORT_MDS)
{
returnUrl.append(";deviceside=false");
}
else if(transportType == TransportInfo.TRANSPORT_BIS_B)
{
returnUrl.append(";deviceside=false;connectionUID=" + transUID);
}
else if(transportType == TransportInfo.TRANSPORT_TCP_CELLULAR)
{
returnUrl.append(";deviceside=true");
}
else if(transportType == TransportInfo.TRANSPORT_WAP2)
{
returnUrl.append(";deviceside=true;ConnectionUID=" + transUID);
}
}
} catch (Exception e) {
// pass
}
return returnUrl.toString();
}
public static boolean quickCheckConnection() {
boolean canConnect = true;
if (RadioInfo.getState() == RadioInfo.STATE_OFF && WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_DISCONNECTED) {
System.out.println("### RADIO IS OFF");
canConnect = false;
}
return canConnect;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment