|
Depending on what you are trying to do it's quite easy to get a simple web service up andd running. Passing simple types back and forth is fairly trivial, complex types is not much more difficult. Where I've had trouble is in passing simple types as input parameters and returning complex types. We'll explore this later on. First lets look at the easiest case, passing simple types as parameters.
First you will need to allow data access fro your BB Simulator. If you don't do this you can expect to receive a network timeout exception. Download, install and run the MDS Simulator. Go to https://www.blackberry.com/Downloads/entry.do?code=060AD92489947D410D897474079C1477. Select 'BlackBerry Email and MDS Services Simulator Package'. Once installed run the 'MDS' application. Next we'll set up the server portion of this tutorial. The service will contain a simple method that takes a String and returns a String. I won't walk through the entire process of creating a .NET web service but here is the code:
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace HelloWorldService
{
[WebService(Namespace = "http://tempuri.org/")]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public String HelloServer(String msg)
{
return "Hello From Server-" + msg;
}
}
} Now let's revist the HelloWorldScreen class from Part 1. final class HelloWorldScreen extends MainScreen
{
private RichTextField textField;
public HelloWorldScreen()
{
super();
LabelField title = new LabelField("HelloWorld Sample", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
textField = new RichTextField("Hello World!");
add(textField);
}
public void setScreenTest(String text)
{
textField.setText(text);
}
public boolean onClose()
{
Dialog.alert("Goodbye!");
System.exit(0);
return true;
}
}Now the HelloWorld class needs a bit of tweaking:
public HelloWorld()
{
pushScreen(new HelloWorldScreen());
String serviceUrl = "http:// <SERVER>/HelloWorldService/Service.asmx";
String serviceNamespace = "http://tempuri.org/";
String soapAction = "http://tempuri.org/HelloServer";
SoapObject rpc = new SoapObject(serviceNamespace, "HelloServer");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
rpc.addProperty("msg", (new Date()).toString());
HttpTransport ht = new HttpTransport(serviceUrl);
ht.debug = true;
String result;
try
{
ht.call(soapAction, envelope);
result = (envelope.getResponse()).toString();
}
catch(Exception ex)
{
result = ex.toString();
}
((HelloWorldScreen)this.getActiveScreen()).setScreenTest(result);
} Now run your code! Ok, great. Now what about passing Complex types back from the server? Again, lets tweak the server side code. Notice the HelloServer2 method. It will return an object named Complex. Also notice that the WebServiceBinding is now set to WsiProfiles.None
namespace HelloWorldService
{
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[WebService(Namespace = "http://tempuri.org/")]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public String HelloServer(String msg)
{
return "Hello From Server-" + msg;
}
[SoapRpcMethod]
[WebMethod]
public Complex HelloServer2()
{
Complex myObj = new Complex();
myObj.name = "foobar";
myObj.value = 42;
return myObj;
}
}
public class Complex
{
public String name;
public int value;
public Complex()
{
}
}
}
Once again, a change to the client side:
String soapAction2 = "http://tempuri.org/HelloServer2";
SoapObject rpc2 = new SoapObject(serviceNamespace, "HelloServer2");
SoapSerializationEnvelope envelope2 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope2.bodyOut = rpc2;
envelope2.dotNet = true;
envelope2.encodingStyle = SoapSerializationEnvelope.XSD;
// Add the mapping so that the return value can be converted back to our Complex obejct.
envelope2.addMapping("http://tempuri.org/encodedTypes", "Complex", new Complex().getClass());
envelope2.setOutputSoapObject(rpc2);
HttpTransport ht2 = new HttpTransport(serviceUrl);
ht2.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
ht2.debug = true;
String result2;
try
{
ht2.call(soapAction2, envelope2);
Complex myObj = (Complex)envelope2.getResponse();
result2 = myObj.getClass().getName() + ", name="+ myObj.name +", value="+myObj.value;
}
catch(Exception ex)
{
//if we get an exception we'll just write the msg to the screen.
result2 = ex.toString();
}
There you have it! Well... almost. There is one thing that I have not been able to get working properly; that is passing a string to a web service as a parameter and returning a complex type. If anyone can find the flaw in my code and can get this working I would appreciate hearing from you! UPDATE: Check out Part 3 of this series to see complex types in action!
Resources: |
Comments
I would like to send an array of objects from the web service to the bb application.
What would I have to do in the bb application?
I have to change the class coming in addMapping. In this case "new Complex().getClass(). Here I dont sure how to detail the array of objects that is coming from the web service.
envelope2.addMapping("http://tempuri.org/encodedTypes", "Complex", new Complex().getClass());
Can I specify an array there?? I read the documentation and it says that it is for a class.
Help please...
thanxs...
it is about set properties in the eclipse.
But now, I have this exception: org.xmlpull.v1.XmlPullParserEx ception:unexpec ted type(position:T EXT Connection refus...@1:30 in java.io.InputStreamRead er@b7ab6c7d)
I have changed the 'localhost' to the system name.
I am using eclipse 3.5 classic and 8310 simulator.
some help please...
First at all thanks for this tutorial...
I have a simple issue here and as you said: "First you will need to allow data access fro your BB Simulator. If you don't do this you can expect to receive a network timeout exception".
Well, I have a timeout exception.
Can you please help me with this issue??
Thank you...
I've installed and run MDS but the simulator still times out....
Please any ideas on why?
This is really a great Tutorial. This tutorial helped me a lot.Thank u very very much.
Just want to say, great work writing this tutorial.
I was just wondering if I could get an idea about parsing the data that I'm getting from the server as a response.
I'm getting XLS anytype: and then "response body" in the data.
Please suggest..
Thanks
I am trying to call Web Service using KSAOP
I have i issue how to pass complex type as property
like i have service to call as
string
string
string
string
string
please help me