An HttpRequest and HttpResponse library for BB OS5+
- August 19th, 2011
- Posted in Projects
- By admin
- Write comment
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.
Hi
This a grat httpResponse lib, but how do you handle the redirections and/or the headers of the response?
I’m not sure what you mean, I seem to recall there being a redirect catcher in the httpRequest java file. It retries each new URL until it reaches maximum redirect property.
Hey, I am new to bb/java development found this code really helpful. I am doing an app using SDK 5.0+ and testing on a bb running OS5 all is well and working. However testing on OS6/7 I am getting a SERVERNOTFOUNDEXCEPTION, now being new to bb/java debuggin the source of this is driving me crazy. Can you check if OS6/OS7 included anything that would cause problems.
also using Simplified Observer Pattern in the app, very nice.
Regards
To be perfectly honest, I have not looked at this code since it was posted. In my OS 5 apps I used it exclusively, but I remember using a response listener in conjunction with this library when I did a few for OS6 and 7. For the response listener example look up the youtube app example.
As far as why this is not working for you… Is your URL being formatted wrong when passed in? Maybe OS6 handles it differently. Does it ALWAYS fail or just sometimes?
Wow, thanks for the reply.
Searched for your youtube app, is the response listener example source available (email).
The ur is correct as far as I can tell, the function works perfect in OS5. its OS6/7 it complains.