Android apps connected to C++Builder DataSnap Server

Categories:Android, C++BuilderTags: , , ,

As part of the 31 days of RAD Studio XE2 Tutorials, in today’s video I’m showing how you can create DataSnap REST Server in C++Builder, expose server methods and call them from an Android application. The video shows all the steps to create the server and the Android application.

This is a short recap of the video.

In order to have other platforms connecting and interacting with your DataSnap Server you need to enable the REST Interface, and for mobile platforms you can use the DataSnap Connectors that generate proxy classes for Android, BlackBerry, Windows Phone and iOS. In the video I’m showing how to create the Server, let’s take a look at the Server Method implementation.

The first server method called InsertCountry, connect on my InterBase database and add a new record to the Country table, see the implementation below:

bool TServerMethods1::InsertCountry(String country, String currency,
	String &error) {
	TDBXCommand *comm;

	bool result = false;
	DbConn->Open();
	comm = DbConn->DBXConnection->CreateCommand();
	comm->CommandType = TDBXCommandTypes_DbxSQL;

	TVarRec vr[] = {country, currency};
	comm->Text =
		Format("Insert Into Country (COUNTRY, CURRENCY) Values ( '%s', '%s')", vr, 2);

	if (!comm->IsPrepared) {
		comm->Prepare();
	}

	try {
		comm->ExecuteUpdate();
		result = true;

	}
	catch (Exception* e) {
		error = e->Message;
	}
	delete comm;
	return result;

}

The second server method is called GetCountries, and returns all the records I have in the COUNTRY table on my InterBase database. This method returns a DBXReader and DataSnap will convert this automatically to JSON at the moment the Android app calls this method, but since we will use the new DataSnap Connector to generate the proxy class, we won’t need to handle the JSON object and I will explain that after.

The following code represents the GetCountries implementation.

TDBXReader* TServerMethods1::GetCountries() {

	TDBXCommand *comm;

	try {
		DbConn->Open();
		comm = DbConn->DBXConnection->CreateCommand();
		comm->CommandType = TDBXCommandTypes_DbxSQL;
		comm->Text = "Select * from Country";

		if (!comm->IsPrepared) {
			comm->Prepare();
		}
		return comm->ExecuteQuery();
	}
	catch (Exception* e) {
		return NULL;
	}
}

The next step is to create the proxy class for Android using the DataSnap connectors, which is a new feature in C++Builder XE2. The generation can be done through command line or via HTTP request on your DataSnap server, and in the video I’m showing how to do that.

The proxy generator generates only the DSProxy.java, all the other files part of the DataSnap Connectors are the representation of DataSnap Framework in Java, for example you won’t need to parse the DBXReader as JSON object in Java, just because we have the DBXReader class in Java and it parses the JSON object for you.

The first step to create a REST connection with your DataSnap Server, which will give me a instance of DSRESTConnection. If your DataSnap Server requires authentication you can use the methods SetUserName and SetPassword to define the credentials.

	private DSRESTConnection getConnection() {
		DSRESTConnection conn = new DSRESTConnection();
		conn.setHost("192.168.254.128");
		conn.setPort(8080);
		return conn;
	}

Now, let’s take a look and see how to execute the server methods, starting with the InsertCountry method as you can see in the following code.

After instantiation of the DSRESTConnection, we pass the REST connection object to the TServerMethods1’s instance, which is the client representation of TServerMethods1 on the server, and from there just call InsertCountry.

Since InsertCountry returns Boolean and also the Error message parameter is passed as reference, InsertCountry methods will return a InsertCountryReturns, which is a static class with two parameters: error that represent the error message and returnValue which represent the value return by InsertCountry.

protected void Insert() {

	DSRESTConnection conn = getConnection();

	TServerMethods1 sm = new TServerMethods1(conn);

	try {
		InsertCountryReturns ret;
		ret = sm.InsertCountry(editCountry.getText().toString(), editCurrency.getText().toString(), "");

		if ( ! ret.returnValue ) {
			System.out.println(ret.error);
		}

	} catch (DBXException e) {
		e.printStackTrace();
	}

	// Hide keyboard
	InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
	imm.hideSoftInputFromWindow(editCountry.getWindowToken(),0);

}

The next method is GetCountries. We start as before by getting the DSRESTConnection instance. The GetCountries method returns a DBXReader and the way we work with the DBXReader class in Java is very similar the way we work in C++, use the Next method to interact through the result, and getValue/GetAsXXXXX to get the column value. See the following code:

protected void Refresh() {

	DSRESTConnection conn = getConnection();

	TServerMethods1 sm = new TServerMethods1(conn);

	TDBXReader reader;
	try {
		reader = sm.GetCountries();

		ArrayList COUNTRIES = new ArrayList();

		while (reader.next()) {
			COUNTRIES.add(reader.getValue("COUNTRY").GetAsString());
		}

		// Add the COUNTRIES array in to the list view
		list.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, COUNTRIES));

	} catch (DBXException e) {
		e.printStackTrace();
	}

}

This is just a short tutorial explaining how to use DataSnap Connector with Android, take some time and watch the video where I’m going through more details.

The source code is available for download here and download the video from here.

8 Responses to Android apps connected to C++Builder DataSnap Server

  1. Reply bjzhq says:

    hi, I do not logon http://www.youtube.com, so I do not see your this video, where can I download this Video? I very like this video. thank you

  2. Reply bjzhq says:

    yes, I do not see Youtube’s anything, because I do not goto it(youtube & facebook are forbided at our place :( ).
    my boss let me try: dataSnapXE Server + android phone, I am glad to see your posting.
    I think that your video must save a lot of my times.
    thank Andreano very much.

  3. Reply bjzhq says:

    thank you very much, i will download it tomorrow at here

  4. Reply bjzhq says:

    thank Andreano very much, I have downloaded this video, It is very useful for me, thanks :-)

  5. Reply Lena says:

    Thank you very much!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>