Call a .NET Web Service in a BlackBerry Widget
URL: http://www.geekreflex.com/blog/blogarticle.aspx?ArticleID=13
One of my responsibilities where I work is to provide custom software for our BlackBerry smart phones. As of June 30, 2010, BlackBerry MDS Runtime will no longer be supported by RIM (Research in Motion). This posed a problem since we upgraded our users to new BlackBerry Bold 9700s this year. The new BlackBerry application platform focuses on two approaches; BlackBerry Widgets and Java. BlackBerry Widgets utilize web development languages such as HTML, CSS, Javascript and AJAX. Since I am not very familiar with Java development, I chose to migrate our existing MDS applications to BlackBerry Widgets.
RIM offers a plug-in for Microsoft Visual Studio that allows .NET developers to use a .NET programming environment to develop BlackBerry applications. The MDS plug-in worked great and provided the ability to reference a .NET Web Service with ease. The good news is, this plug-in has been updated to support new BlackBerry Widgets, however it’s still in Beta form. If you need to reference a web service in a BlackBerry Widget app you’ll notice immediately that it’s not as easy in the new web plug-in as was in the MDS plug-in. Not to worry though. With a little bit of AJAX, you can consume a web service without any problem and I’m going to explain and demonstrate how to do this from start to finish.
For this example, we’ll do the following:
- Create a BlackBerry Widget project in Microsoft Visual Studio 2008.
- Create a .NET Web Service with a simple “Hello World” method.
- Invoke the web service method in the BlackBerry Widget app using XMLHttpRequest.
First, you’ll need a couple of things before you can create a BlackBerry Widget; the BlackBerry Web Plug-in for Visual Studio and the Sun Java Development Kit 1.6 or Higher. Follow this link to get the Plug-in and JDK. The Web Plug-in for Eclipse is also provided, but for this example we’ll be using the Visual Studio Plug-in.
Once you have the Plug-in and JDK installed, open Visual Studio 2008 and Create a new Project. Here you should simply be able to select the BlackBerry Project type, then BlackBerry Widget and hit OK. However, since the Plug-in is still in Beta it’s a bit buggy. Before you can actually save a BlackBerry Widget project you have to create a separate project type first, and then add a BlackBerry Widget project as a new Project. If not, you’ll be presented with the following error when trying to save the project.
I didn’t research this issue so there could be other work-a-rounds and it’s possible you may not experience this error at all. I did encounter this error and I’m using Visual Studio Team System 2008 Development Edition and the MDS plug-in had also previously been installed.
So…instead of selecting the BlackBerry project type, expand Other Project Types and select Visual Studio Solutions. Select the Blank Solution Template, name it “HelloWorldWidget” or whatever you desire and click OK. Now, right-click on the Solution in the Solution Explorer and Add a New Project. This time, select the BlackBerry Project type, then the BlackBerry Widget Template. Again, name it “HelloWorldWidget” and click OK.
Two files will be created and added to your Widget project; config.xml and index.htm.
Before we go any further, let’s go ahead and create the Web Service. Add another New Project to your Solution. Under Visual Basic (used in this example) or Visual C# Project type, select Web and create an ASP.NET Web Service Application. Call it “HelloWorldService”. By default, a HelloWorld Web Method is already added to Service1.asmx.vb. If for whatever reason it’s not, add it.
Right click the Web Service in Solution Explorer, select Debug and Start new instance This will create a test ASP.NET Development Server we can use to call our service later.
Now let’s get back to the Widget. View the code of index.htm and add a simple input button to the body.
Eventually, clicking this button will display an alert box with the return string, “Hello World” from our web service. To do this, we just need a little AJAX script.
To keep our project neat and organized, create a New Folder in the Widget app and call it “scripts”. Right click the folder and select Add > New Item…. Select JScript from the BlackBerry category and name it “actions.js”.
Here we’re going to use XMLHttpRequest make a request to our web service and invoke the HelloWorld method. We’ll retrieve the response in XML, then parse the XML to retrieve our return string.
First, create the XMLHttpRequest object.
Now, you’ll need an event handler which will be called from the button click on the htm page. This will send the request for data to the web service. We’ll make the request by sending a GET method to the web service.
Make sure and change the url above to your test ASP.NET Development Server url.
The sayHello() function listens to the onreadystatechange property of the XMLHttpRequest object and calls another function that we’ll call callbackFunction(). This function determines when the server has completed our request and will handle the data returned.
The call back function checks for a readyState value of 4. This value indicates that the request has completed and we can use the data returned. There are other values that may be returned from this request but for this purpose we are interested in this value.
We’ll receive the data as text through the responseText property of the XMLHttpRequest object and then display it in an alert box.
Just a few more steps and we’re done! Open your config.xml file in your BlackBerry Widget application. Here you have several different configuration options you can change, however there is only one we are required to change before our widget will work correctly. We’ll need to give our widget access to the domain where our web service resides. Click Add Domain, then enter the domain into the textbox provided. Our web service is on our local machine so we’ll use http://localhost:2064. For this example, the ASP.NET Development Server is using port 2064, but this may be different on your machine.
Now let’s test it out! Make sure the widget application is set as the StartUp Project, then hit F5 (or start the debug instance from the GUI). The BlackBerry simulator should start loading and you will eventually be presented with your application. The BlackBerry Simulator is a pretty cool tool, as it does very well in simulating a BlackBerry device. You can also download other BlackBerry models to simulate from BlackBerry’s website.
Test the widget out by clicking the button that you created. Nothing happen? Good. We still have one more step before we can get a response, sorry. I wasted a lot of time trying to figure this one out, so I intentionally left it out above so it would stand out. We have to enable our web service to be invoked using SOAP and GET, as ASP.NET does not allow this by default. To do this, simply add the following code under system.web in your web service’s Web.config file.
Alright, now let’s run our widget application again! This time you should receive an alert box similar to the following after pressing the button:
Don’t worry, we’re still not finished. Our code at this point is just returning the XML from our web service method as plain text. You have a few options here. You can parse this text to retrieve the actual value, or you can return the data as XML using the responseXML property of the XMLHTTPRequest object. Then we can parse the XML with ease to get our “Hello World” string. Change your javascript callbackFunction() to look like the following:
If you want to learn more about XML Parsing, check on this link
Now the Widget application should display an alert box with a simple “Hello World” string.






