2
votes

I'm building an app for the iPhone linking to a Delphi XE2 DataSnap REST server.

Originally I was coding the iOS front-end with the help of Embarcadero's mobile connectors, however due to many bugs I'm having to write my own link with the help of asihttprequest.

Whilst I can get the system to handle JSON requests and pass one or two parameters (using the URL) such as: -

NSURL *theURL = [NSURL URLWithString:@"http://192.168.1.2:8080/datasnap/rest/tservermethods1/SaveMileage/type1/100];
request = [ASIHTTPRequest requestWithURL:theURL];

(where the server method is SaveMileage and the two parameter values are type1 and 100)

However if I want to add more complex parameters I've needed to use appendPostData to post the parameters using JSON: -

[request appendPostData:jsonMutData];

Below is the output of the jsonMutData: -

{
  "client_ref" : "ABC100",
  "mileage_type" : "Fee Work",
  "mileage_description" : "test",
  "mileage_id" : "0",
  "mileage_date" : "03\/15\/2012"
}

However when I post this to my server via the app I get the the following response back and the call isn't handled by the DataSnap server: -

{"error":"**Cannot convert JSON value** {\"client_ref\":\"ABC100",\"mileage_type\":\"Fee Work\",\"mileage_description\":\"test\",\"mileage_id\":\"\",\"mileage_date\":\"03\/15\/2012\"} input into TDBXTypes.WideString"}

Below is the structure of the DataSnap function: -

function TServerMethods1.updateSaveMileage(mileage_id: string; 
client_ref: string; mileage_date: String; mileage_type: string; 
mileagefigure: string; description: string; notepad: string): string;

Any ideas on how to resolve this?

1
Could you solve the problem? I am having the same issue...henrique romao

1 Answers

1
votes

I know that this question is a bit old, but I was having the same problem and I couldn't find an answer as well.

Luckily, after a couple of hours I finally figured out how to solve this problem.

All you need to do is to modify your function to accept a TJSONObject, as follow:

function TServerMethods1.UpdateMethod(AJSONObject: TJSONObject): TJSONObject;
var
  JSONText: string;
begin

  JSONText := AJSONObject.ToJSON;

  // code...

end;

Tested on Delphi 10.1 Berlin.