DataSnap Mobile Connectors in RAD Studio XE2

WARNING! I’ve been authorized by EMBARCADERO to write about RAD Studio XE2.

RAD Studio XE2 is full of nice and exciting features. One of the most interesting IMHO is the DataSnap extension called “Mobile Connectors”.

In the past, I’ve talked about connecting and using your datasnap REST service with Android, creating ad-hoc json messages and manually parsing the returned json messages. With RAD Studio XE2 this is no longer needed. If you have a DataSnap REST service, you can automatically generate the proxy connector for the major mobile platforms. Yes, just like you have been doing with Delphi or C++ since Delphi 2010.

DataSnap XE2 version supports 4 mobile platforms:

  • Android (Using Java)
  • BlackBerry (Using Java)
  • Windows Phone (Using C#)
  • iOS 4.2 (Using ObjectiveC)

If you want to enable your DataSnap server for the Mobile Connectors you have to explicitely check the feature in the “New DataSnap Server” wizard.

The generated proxies support all the standard Delphi types and maps them to the native target language. Some of the most used Delphi types (e.g. TStream, TDBXReader and so on) have been rewritten in the target language to allows a greater compatibility and a simpler programming interface. The functionalities of the various Delphi classes are not-one-to-one with the Delphi version, but  similar.

From a remote (or local) machine you can download the generated proxy and all the required files using a tool called “Win32ProxyDownloader.exe” which is in the bin folder of your RAD Studio installation. In my FieldTest version, this tool called without parameters, shows its help.

As usual you should have the RAD Studio bin folder in your PATH environment variable, so you can change your current directory to where you want the proxy and write this command in a commandprompt window:

Win32ProxyDownloader -language java_android -host localhost:8080

The proxy and all the needed files are ready in the current directory.

Mat DeLong wrote a very nice Eclipse plugin to use the proxy downloader directly from Android or BlackBerry development environment. You can find this plugin here.

You know that Android is my preferred mobile platform, don’t you?

So, let’s go with an Android example.

To use the generated java proxy, in an Android client application I can write something like this:

//Create the connection
connection = new DSRESTConnection();
connection.setHost("10.0.0.2");
connection.setPort(8080);
connection.setProtocol("http");
//Create the proxy
proxy = new DSProxy.TServerMethods1(connection);
//Use a simple remote method
int sum = proxy.Sum(1,4));
//Use a complex remote method
TStream inStream = null;
TStream outStream = null;
String s = "abc";
inStream = new TStream(s.getBytes());
outStream = proxy.DoSomethigWithATStream(inStream, sum, "Hello DataSnap Mobile Connectors");
//here I can use the java TStream type

All the custom Delphi types (e.g. TPerson) are mapped on the target platform as TJSONObject. All the TJSONValue hierarchy has been ported, with a very similar interface, to the target platform as a wrapper of the native JSON classes.

So, you can write code as the following (Java on Android):

TJSONObject jobj = new TJSONObject();
jobj.addPairs("firstname", "Daniele");
jobj.addPairs("lastname", "Teti");
jobj.addPairs("age", 31);
jobj.addPairs(new TJSONPair("nickname", new TJSONString("Spiderman")));
if (jobj.has("firstname"))
  doSomethingWithFirstName(jobj.getString("firstname"));
doSomethingWithAge(jobj.getDouble("age").intValue());

All the proxies work in a similar way except for the Windows Phone one. Indeed, the WP proxy is asynchronous because Microsoft does not allow a sinchronous http request in the main thread. All the proxies are thread safe.

The proxies are generated on the fly by a set of specialized writers. The TDSProxyGenerator component is in charge of generate the actual proxy code in the target language/platform using one of the specialized generators.

In the next figure you can see all the available proxy generators. Some of them are there since Delphi XE but all the mobile platforms have been added in XE2.

That’s all for now.

RAD Studio XE2 will be officially presented all over the world during the “RADStudio XE2 World Tour”.

You can find the list of all launch events in the RADStudio XE2 World Tour page.

I’ll be a presenter at 3 launch events in Italy and United Arab Emitates.

These are the events where I’ll be (click to register):

RAD Studio XE2 had a lot of new features. This is really the BEST ever Delphi version since version 1.

I’ll blog about other XE2 features mostly Delphi related (as usual) so stay tuned.

Comments

comments powered by Disqus