Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Sample HTTP Connection code and BIS-B Access

[ Edited ]

There are numerous samples of http connection code on this forum and in other places.  In addition there are numerous places that talk about how to connect from a BlackBerry to a Server.  Despite this, it is clear from questions asked on this forum, that people find still find connecting a BlackBerry difficult.  In addition, when the connection breaks, people find it difficult to determine what the problem is.

 

To help, especially inexperienced Blackberry developers. I have created the attached sample code.  This code should, as of now, be considered 'beta' code, I still need to do some testing, I need to complete the 'background processing' and I am waiting some reviews/comments.  But this code is not helping anyone sitting on my workstation.  So rather than delay further, I am publishing in the hope it will help someone now and hopefully you will comment on or criticize this code so that I can improve it.

 

The code addresses the most common issues I have with samples that people provide

a) They don't get off the Event Thread

b) They don't log the actions that occur

 

In addition this code handles common http problems like basic authentication and redirection.

 

The code is presented in two parts, the test screen and the library.

 

You should start by reviewing the test screen and seeing how it invokes the library.  There are three 3 statements:

 

1) connectionType = HttpLibrary.chooseConnectionType("Test Connection",
                "Choose Connection Method to obtain Test");

 

This invokes the Library which will determine the connection types supported and ask the user which one they would like to try.  You don't have to do this.  You can decide which connection type you want to use yourself and just try that.

 

2) HTTPRequestRunnable getData = new HTTPRequestRunnable(_urlToTry.getText(), postData, connectionType, true);

 

This creates the 'request' that is to be processed - you pass in the URL, the postData (if any), the connection type to use (see (1)) and whether the processing is foreground or background.  This last flag is still being worked on, it may be dropped so that the Library can figure it out for itself based on whether the the processing is being called on the Event Thread or not.  But I haven't finished that development yet.

 

3) if ( HttpLibrary.getResponse(getData, "Testing") < 0 ) {

 

This will return when the request created in (2) has completed, a -1 indicates the request was cancelled by the user.

 

This can be called on the Event Thread, for example in the middle of some menu processing.  It pops up a 'wait screen', which the user can cancel.  When this completes (assuming it has not been cancelled), you can query the HTTPRequestRunnable object "getData" to determine what actually happened.  And you can obtain a log of the events that happened, including the exceptions.

 

So please start by reviewing the TestScreen and understanding how it works.  I will talk about the Library in the next Post. 

 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Sample HTTP Connection code and BIS-B Access

Once you are happy with the Test Screen, it is time to look at the Library.

 

The first point to make is that I do not expect you to use this as a "Black Box" Library.  Please copy this code into a package in your project and rename appropriately.  You should do this, and change things like the GUID associated with this, so that it will not conflict with another developer's version of this same code. 

 

The Library uses two functions that you probably already have:

 

1) A Repository.  In the Library it is called HttpRepository.  Have a look at it and how it is used.  I suspect most projects will have a similar place to store shared data, so this can probably be removed and the HttpLibrary package can use the projects Repository

 

2) A Logger.  Pretty much as above, if you are already logging in your own application, then redirect the logging requests for the Http connections into your current logger. 

 

You may find other things you want to move or change, such as:

a) The cycling icon

b) the Utilities class.

 

There are some oddities in there too, such as the use of the Stoppable Interface (a carry over from my PleaseWaitPopupScreen KB articles).

 

And you will note that two classes have been copied (and slightly modified), with permission, from the Network Diagnostics Tool.  TransportDetective and URLFactory.

 

I just need to spend a little time on ConfirmConnectionTypePopupScreen.  You will note that it really only supports 3 types of connection:

a) BIS-B

b) BES/MDS

c) WiFi

 

This is because for most people who are doing simple http requests, these are the most suitable connection methods.  Direct TCP and WAP both have potential to be screwed up by transcoders, as well as requiring extra connection parameters.  This sample code is not intended for people attempting to do streaming. 

 

The code in this screen (which is not too pretty, I'm sorry), attempts to make a judgment about which one of these it will use and asks the user to choose from the options that are supported on device. 

 

Ah but, I hear you say, how can we get BIS-B - I am not an Alliance member?  Without BIS-B this code won't work on consumer phones.  You are right, for that you will have to wait for the next post. 

 

And if you don't understand BIS-B. BES/MDS and WiFi, then you are going to have sit down and do some reading.  Start here:

http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-con...

and look here:

http://supportforums.blackberry.com/t5/Java-Development/What-Is-Network-API-alternative-for-legacy-O...

not forgetting this:

http://devblog.blackberry.com/2010/11/network-connectivity/

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Sample HTTP Connection code and BIS-B Access

[ Edited ]

For those of you that have been chasing easy network connectivity, you will know that BIS-B is held out as the saving grace, especially for consumer devices.  It is the connection used by the Browser and by BBM.  So it is available on all consumer phones.  But to use this connection method, you had to be an Alliance member and you had to be a certain level, which costs.  

 

But I must emphasize that even Alliance members have to ask for BIS-B access, and have to identify, among other things, the volume of traffic and the target IP addresses.  I presume RIM uses this information to size the BIS-B infrastructure that they have in place. 

 

Access to BIS-B to non upper level Alliance members has been promised for sometime.  Unfortunately it would appear this has ground to halt on the RIM legal framework. 

 

But you can get BIS-B access.  You just need to ask for BIS Push for your application. 

 

When you are applying for BIS-B Push, you have to answer pretty much the same sort of questions as you do as an Alliance member asking for BIS-B.  So RIM can use this information to size their network.  And they can check that only the agreed URLs are being hit and these are within the volumes you have stated. 

 

Now I must be honest, I have never done this.  But I have helped a forum user who has, And I have confirmed with RIM that, while this is potentially a 'back door' to BIS-B, they are happy to leave the back door open while they struggle to unlock the front door (not sure this is a good analogy, but hopefully you get the idea). 

 

Anyway, you ask for BIS Push, you get it, you get access to some samples, and the samples include the connection string you need for BIS-B.  You put that string in URLFactory and you are ready to roll.

 

Yee ha...

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Sample HTTP Connection code and BIS-B Access

Finally some words of thanks....

 

This code would not have been created without Shadid Haque's Network Diagnostic Tool.  Shadid has done a brilliant job with the Network Diagnostic Tool over the years and I think this last iteration is the best yet.  Thanks.

 

I must also thank Mike Kirkup who has been trying to get us BIS-B access forever.

 

Mark Sohm and Simon Hain have both cast their eyes over this code.  But of course I've changed it since they last looked at it, so any bugs etc. are mine.

 

And there are numerous people with whom I have worked over the years on network related problems in this forum. Thanks. 

 

Ok why am I going on here?  Well I am really really hoping this I can use this as a solution to this long winded monstrositoty:

 

http://supportforums.blackberry.com/t5/Java-Development/Connecting-your-BlackBerry-http-and-socket-c...

 

And it feels like the end of an era. 

Please use plain text.
Developer
simon_hain
Posts: 15,953
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Sample HTTP Connection code and BIS-B Access


peter_strange wrote: 

And it feels like the end of an era.


Well, let us all hope it is.

 

 

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Sample HTTP Connection code and BIS-B Access

Depends on what era you specify. You have done more to help people here with network issues and connections then anyone could have imagined. Thank you for that and I expect this to help many others.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
luisfernando
Posts: 91
Registered: ‎11-24-2009
My Device: Storm

Re: Sample HTTP Connection code and BIS-B Access

Kudoed all your post on this thread because is true what rcmaniac25 said. Your contributions here are highly appreciated.

 

(thanks too to simon_hain and rcmaniac25)

-Luis Fernando

Remember to give Kudos(click on the star at your left) if this helped you.
Click on "Accept Solution" if the problem is resolved.
Espanol? Escribeme un mensaje privado.
Please use plain text.
Contributor
myamada
Posts: 46
Registered: ‎05-21-2010
My Device: ...

Re: Sample HTTP Connection code and BIS-B Access

This could prove incredibly useful, thanks for sharing.

 

What license are you releasing this code under? I didn't see any mention in the zip.

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Sample HTTP Connection code and BIS-B Access

Good point, someone else mentioned this directly to me, but I forgot to action this.

 

With the exception of TransportDetective and URLFactory, this code is available with no license or restrictions at all.  You can use this as you like, where you like.  There is no need to acknowledge the authorship.  You can redistribute this, or modify this as you like, and distribute the executable file as a proprietary software product, or a free product as you like.

 

Basically, use this as though you wrote it. 

 

It would be great if people using this code could do the following:

1) if you find a bug, then please let me know

2) if you redistribute a pretty much unaltered copy of the source, that other people might use, then it might be appropriate to reference back to here, only so that rather than use your copy, people can find the source, with any 'fixes' that have been applied. 

 

However this does not apply to TransportDetective and URLFactory, because I did not actually write these (though I have modified them with consent).  They are part of the Network Diagnostic Tool.  You can find this tool here:

http://supportforums.blackberry.com/t5/Java-Development/What-Is-Network-API-alternative-for-legacy-O...

I suspect it has some documentation related to licensing in it.  That said, I will see if I can some official comment specifically regarding the two classes I have included in this Sample. 

Please use plain text.
Contributor
myamada
Posts: 46
Registered: ‎05-21-2010
My Device: ...

Re: Sample HTTP Connection code and BIS-B Access

I wondered about the Network Diag tool's licensing too actually.

 

Have you considered putting it on Github or something to easily track bugs and patches, etc?

 

Anyway, thanks again Peter.

Please use plain text.