BlackBerry and .NET Web Service Tutorial - Part 1 PDF Print
Monday, 22 June 2009 17:24

I have recently been trying to write a BlackBerry application to consume a .NET web service. It wasn't as trivial as I had first thougt. It wasn't the code that was challenging,  for me it was the environment setup. By cobbling together various bits and pieces from around the net(see references below) I was able to get my application running. I've documented my experience here. This tutorial will take you from a clean machine to a working BlackBerry app with .NET web service.

 Setting up Eclipse and Installing KSoap2 on the BlackBerry 

First of all, do yourself a favour. Just download the Full JDE Plugin, it includes Eclipse. I started by installing a vanilla Eclipse and getting the plugins after, it wasn't worth the hassle for me. I'm using version 1.0.0.67

 Now fire it up and let's start.
  1. Create a new workspace.
    gallery thumbnail
  2. Create a new BlackBerry Project. File > New > Project Select BlackBerry project named ‘Sample’.(Figure 2)
  3. Create another new BlackBerry Project named 'KSoapLibrary' (Figure 3). 
    gallery thumbnailgallery thumbnail
  4.  Open the Project Properties for KSoapLibrary. Go the 'BlackBerry Project Properties' and click the 'Application' tab. From the Project Type drop down select Library (Figure 4). Click OK. 
    gallery thumbnail
  5.  Now we need to add the KSoapLibrary as a project reference to the Sample project. Go to the Project Properties for Sample, click the Project References and select the KSoapLibrary checkbox (Figure 5).  Click OK. 
    gallery thumbnail
  6.  It's time to create a class within our Sample project. Select New > Class. Set the class and packages names (Figure 6). Click Finish. 
    gallery thumbnail
  7. Add the contents of the HelloWorld.java file found in the Resources section at the bottom of this page to your new class.
  8. Eclipse will now give you a number of errors in the file (Figure 7).  This is because Eclipse does not have the KSoap files within the build classpath. Let's fix that. 
    gallery thumbnail
    • Create a folder within the KSoapLibrary project named “lib” and add the ksoap2-j2me-core-prev-2.1.2.jar file to the new folder. The pre-verified ksoap file is VERY important. You can either preverify it yourself ordownload ksoap2-j2me-core-prev-2.1.2.jar now
    • In the Sample project properties. Click on the ‘Java Build Path’ item, then the ‘Libraries’ tab. Finally, click ‘Add Jars…’. Select the KSoap jar, then click OK. Now click on the “Order and Export” tab and select the KSoap jar and click OK (Figure 8). 
      gallery thumbnail
    • Now your Eclipse Package Explorer should like the screen shot below, and the error will have disappeared (Figure 9). 
      gallery thumbnail
  9. Finally, create another folder under your Samples project named ‘images’ and add an icon to it. Now go to your project properties then “BlackBerry Project Properties” and add some information to the ‘General’ tab and add your icon from the ‘Resources’ tab (Figure 10).
    gallery thumbnail
  10. Done! Now Run it! 
    gallery thumbnail

Didn't work? 

Common problems may include “Error Starting Sample: Module ‘ksoap2-j2me-core-prev-2.1.2’ not found.” I struggled with this for some time. Go back and retrace your steps, you’ve skipped over something. Even though it compiles fine within Eclipse the ksoap jar has not been deployed to the device along with the rest of your code. I have noticed an annoyance where the KSoap jar will be removed from the Properties > Java Build Path “Libraries” and “Order and Export” tabs if you make any further changes to the “BlackBerry Project Properties”. You’ll have to go back in and re-add the jar.
gallery thumbnail

 The next example will build off of this and demonstrate how to connect to a .NET web service.

Resources: 


 

Comments 

 
0 #24 İbrahim ULUDAĞ 2010-07-19 09:12
Hi,

Thanks for this tutorial.
Quote
 
 
0 #23 Danilo Andrade -Bean 2010-07-05 18:35
Best tutorial EVER for this problem!
Quote
 
 
+1 #22 Chris 2010-06-29 15:56
Best tutorial EVER for this problem! I've read a lot of tutorials for adding 3rd party APIs and they always seem to leave out sommething! Thanks a bunch!! :-D
Quote
 
 
0 #21 chin 2010-06-18 02:45
when i build the Source Code for this tutorial...i have 1 error...

C:\Users\admin\ Desktop\bb application\sam pleworkspace\Sa mple\src\com\cr aigagreen\sampl e\HelloWorld.java:38: cannot find symbol
symbol : class Date
location: class com.craigagreen.sample.HelloWorld
rpc.addProperty("msg", (new Date()).toString());
^
1 error
Error!: Error: java compiler failed: javac -source 1.3 -target 1.3 -g -O -d C:\Users\admin\ AppData\Local\T emp\rapc_5bb42e cd.dir -bootcla ...
Error while building project

what should i do to solve this error???
can u help me?

thanks
Quote
 
 
0 #20 gunther 2010-06-01 03:23
Hi i have a problem and i cant solved...

i try to use the ksoap2 for contect to a webservice un .net, i use the tutorial and make to all---

I use eclipse whit full plugin blackberry 5.0.. but when compile show me this error!!

B:\Program Files\Eclipse\p lugins\net.rim.ejde.componentpack5. 0.0_5.0.0.25\components\b in\rapc.exe -quiet codename=deliverables\St andard\5.0.0\apptestdelive rables\Standard \5.0.0\apptest.rapc -sourceroot=B:\blackberry\g unther\workspac e\semana\ src;B:\blackber ry\g unther\workspac e\apptest\res -import=..\Ksoap\delivera bles\St andard\5.0.0\Ksoap. jar;B:\blackber ry\g unther\workspac e\Ksoap\lib\ksoa p2-j2me-core-prev-2.1.2.jar;B:\Program Files\Eclipse\p lugins\net.rim.ejde.componentpack5. 0.0_5.0.0.25\components\l ib\net_rim_api.jar B:\blackberry\g unther\workspac e\apptest\bin
I/O Error: Import file not found: ..\Ksoap\delivera bles\St andard\5.0.0\Ksoap.jar

what happend!!! thanks for the advance
Quote
 
 
0 #19 eesbee 2010-05-12 03:19
hi craig,

how about sending dataset schema request to a .net web service server?.. im having a hard time figuring in ksoap2.. could u give me some idea on how to do it?

thanks in adavnce!
Quote
 
 
0 #18 RoshanThecho 2010-03-17 09:36
Here is the code:

package com.test.sample;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializati onEnvelope;
import org.ksoap2.transport.HttpTransport;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class MyWebServiceTes t extends UiApplication {
public static void main(String[] args) {
MyWebServiceTes t theApp = new MyWebServiceTes t();
theApp.enterEventDispa tcher();
}

public MyWebServiceTes t() {
pushScreen(new HelloWorldScree n());

String serviceUrl = "http://localhost:2715/Service1.asmx";
String serviceNamespac e = "http://tempuri.org/";
String soapAction = "http://tempuri.org/HelloWorld";

SoapObject rpc = new SoapObject(serv iceNamespac e, "HelloWorld");

SoapSerializati onEnvelope envelope = new SoapSerializati onEnvelope(
SoapEnvelope.VER11);

envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializati onEnvelope.XSD;

HttpTransport ht = new HttpTransport(s erviceUrl);
ht.debug = true;

try {
ht.call(soapAction , envelope);
String result = (envelope.getResponse()).toString();
((HelloWorldScr ee n) this.getActiveScreen ()).setScreenTest(r esult);
} catch (Exception ex) {
String msg = ex.toString();
System.out.println(msg);
((HelloWorldScr ee n) this.getActiveScreen ()).setScreenTest(m sg);
}
}
}

final class HelloWorldScree n extends MainScreen {
public HelloWorldScree n() {
super();
LabelField title = new LabelField("HelloWorld Sample",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title) ;
add(new RichTextField("Hello World!"));
}

public void setScreenTest(S tring text) {
add(new RichTextField(t ext));
}

public boolean onClose() {
Dialog.alert("Goodbye!");
System.exit(0);
return true;
}
}
Quote
 
 
0 #17 RoshanThecho 2010-03-17 09:34
This is a nice article. I followed every steps mentioned above. I am using eclipse 3.4.1 and BlackBerry 8380 Simulator and running MDS Service Simulator 4.1.2 in back.

While running, it throws this exception
org.xmlpull.v1.XmlPullParserEx ception: unexpected type (position:TEXT Not authorized t...@1:39 in java.io.InputStreamRead er@e8f86cde)


While debugging, after executing this line
ht.call(soapAction , envelope);

It shows following message in console and goes to catch block with same exception


Console:

Refreshing connection on send
Running refresh
Local port: 19780
APN: ''
APN username: 'null'
APN password: 'null'
Connection does not exist
Detected information for 1 GPAK connection(s)


Exception:

org.xmlpull.v1.XmlPullParserEx ception: unexpected type (position:TEXT Not authorized t...@1:39 in java.io.InputStreamRead er@e8f86cde)

and also control goes to another file (Object.class) and that file says "Source not found" with a button "Attach Source ..."

Do I need to attach any file? What are those username and password in cosole?

Please help me.
Quote
 
 
0 #16 Thechomi 2010-03-16 14:41
I followed every steps as mentioned above. But while running the result of webmethod call doesnot show. While debugging, it throws Null exception in this line.

ht.call(soapAction , envelope);

What was the problem?

Here is my code:

String serviceUrl = "http://localhost:2715/Service1.asmx";
String serviceNamespac e = "http://tempuri.org/";
String soapAction = "http://tempuri.org/HelloWorld";
String serviceMethod = "HelloWorld";

HttpTransport ht = new HttpTransport(s erviceUrl);
ht.debug = true;
ht.setXmlVersionTa g("");

SoapObject rpc = new SoapObject(serv iceNamespac e, serviceMethod);

SoapSerializati onEnvelope envelope = new SoapSerializati onEnvelope(
SoapEnvelope.VER11);

// envelope.setOutputSoapOb ject(rpc);

System.out.println("Created envelope");
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializati onEnvelope.XSD;
/*
* envelope.encodingStyle = SoapSerializati onEnvelope.ENC;
* envelope.encodingStyle = SoapSerializati onEnvelope.XSI;
*/

// rpc.addProperty("Celsius", "12");
String result = "";
try {

ht.call(soapAction , envelope);
result = (envelope.getResponse()).toString();
System.out.println(result) ;// for debugging
net.rim.device.api.ui.component.Dialog.alert(envelope
.getResponse().toString());

} catch (XmlPullParserE xception ex) {
String msg = ex.toString();
System.out.println(msg);
} catch (Exception ex) {
System.out.println("i am here in catch");
System.out.println(ex.getMessage());
// String msg = ex.toString();

}
Quote
 
 
0 #15 Shashank Singh 2010-03-14 22:02
Thanks alot for this tutorial Craig.. It was very useful.

thanks once again for your help and great contribution..
Shashank
Quote
 

Add comment


Security code
Refresh

Sponsored Links