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:
{codecitation class=”brush: c#;” width=”500px”}
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;
}
}
} {/codecitation}
Now let’s revist the HelloWorldScreen class from Part 1.
{codecitation class=”brush: java;” width=”500px”}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;
}
}{/codecitation}
Now the HelloWorld class needs a bit of tweaking:
{codecitation class=”brush: java;” width=”500px”}
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);
} {/codecitation}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
{codecitation class=”brush: c#;” width=”500px”}
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()
{
}
}
}
{/codecitation} Once again, a change to the client side:
{codecitation class=”brush: java;” width=”500px”}
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.
&n
bsp; 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();
}
{/codecitation}
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!