A simple thing that is seen all over stack overflow with constant questions is how to do an http request. I’ll admit this library probably is not perfect, but it is quite in depth and you should be able to find anything you need inside of it. If you want to use it with the classes included in this post and this post download the extra package. If you do not, you’ll need to remove the references to the Logger class from the HttpRequest library.

HttpRequest - HttpRequest BlackBerry library

Javadoc - Javadoc for the library

Extra – extra package for Logger and ObservedThread HttpRequest implementation

 

Below is the usage for downloading a simple image file:

1
2
3
4
5
6
EncodedImage image;
HttpRequest request = new HttpRequest(_imageUrl);
HttpResponse response = request.getResponse();
if (response.getMimeType().equalsIgnoreCase("image/jpeg")){
	image = EncodedImage.createEncodedImage(response.getResponseBytes(), 0, -1);
}

Note that this class will only attempt connections that are available and have enough signal strength. You can use the “Transport” class to check different transports or get a list of available (service book exists and signal strength is high enough, i.e.  getFunctionalTransports() ) connections.  For example “Transport.isWifiSupported()” obviously tells you whether wifi is supported.

 

Related Posts: