Delphi XE5 Mobile REST Client Demo Source

With the release of Delphi XE5 I’ve made the source of the Mobile REST Client Demo available. This is a really super simple demo of the Mobile REST Client in XE5. It is designed to show up how you can consume a JSON REST Service and adapt it into a DataSet and then bind that to the UI. If you change the data source you will probably need to change the live binding.

I’ve changed the demo from the one I used in the video to consume an OData data source. OData is a new standard backed by Microsoft for sharing data over the web. You can think of it as SQL for the web. It is a combination of other technologies, including REST, AtomPub, and JSON. Sybase supports OData on all of their databases now, as does Microsoft and others.

The change was simply a matter of pointing it to the Northwind OData endpoint provided on Odata.org. The binding is setup to show the company name in the listview. I added the ability to specify a Root Element, but that isn’t necessary for the demo.

This new technology works in both desktop and mobile, FireMonkey and VCL. It should also work in C++ Builder.

[Download the demo] (MobileRestXE5.7z 8 KB – Requires Delphi XE5 – Builds for iOS, Android or Win32)

This entry was posted in Delphi Projects, REST, Source Code and tagged . Bookmark the permalink.

40 Responses to Delphi XE5 Mobile REST Client Demo Source

  1. CJ says:

    Very nice, Jim.

  2. Keith says:

    Thanks Jim,
    I was just looking for this!

  3. Chatchai says:

    Hi Jim. Very nice example. However, this is a Mobile consuming XML example. Could you please do the REST server and Mobile consuming the rest result from the REST server both JSON and XML please. I’m very new to this. I’m try to get the idea how it works.

  4. Jim McKeeth says:

    @Chatchai This example is using JSON, not XML. The new TRESTClient is based on JSON and does not introduce any additional XML support.

  5. Chatchai says:

    Hi Jim. I followed the link in the demo and the link is as below
    http://services.odata.org/Northwind/Northwind.svc/Customers
    on the chrome browser it gave me xml format document not a JSON format. Every body is very new to TRESTClient component since it has just been introduced in XE5. I have tried it and still not quite get it yet. I was just wondering if you could do a complete demo of REST sever and REST mobile consuming the server, could be a simple sqlite db on the sever? that would be great thanks.

  6. Jim McKeeth says:

    @Chatchai The browser displays the XML by default, but the client gets JSON. It requests JSON in the HTTP Header. There will be some TRESTClient sessions during CodeRage 8.

    Keep in mind that the new features is the TRESTClient, which is only client side. On the server you could use DataSnap’s REST support to build the server.

  7. Chatchai says:

    Hi again Jim,
    I have never deal with datasnap in my project yet. I’m trying to follow the video from Embarcadero using dbxreader and the return was in JSON format but it is in format as below

    {“result”:[
    {table:[
    [“Field1”,1,0,0,036,35,0,true,false,false],
    [“Field2”,1,0,0,036,35,0,true,false,false],
    “Field1”:[“1″,”2″,”3”],
    “Field2”:[“a”,”b”,”c”]
    ]}
    ]}

    which is not a normal look of json that I used to see. So, I tried with JSONArray and it still and the result is as below

    v <–here is where I have a problem with
    {"result":[ [
    {"Field1":"1","Field2":"a"},
    {"Field1":"2","Field2":"b"},
    {"Field1":"3","Field2":"c"}
    ] ]}

    looks like it removed the word "table" out and still leave an extra "[]" when I try to populate the list from the json result it gave me a blank item when I set the rootelement to "result". So, I was just wondering it this is normal.
    With the jquery way that Marco Cantu made a document for Delphi 2010 it was OK. jquery could handle the selection. I was just wondering if the RESTResponseAdaptor can do something here.

    I'm not new to Delphi but I just haven't got a chance to use new features that Embarcadero added to it yet and I'm looking forward to use it.

    I thank you very much for taking your time response my studying question. I'll check the CodeRage 8 out. Cheers.

  8. Waki says:

    Hello, i have crash when this program running on simulator iPhone ? If component ClientDataSet set Active to False, then program run, but ListView not filled, how to fix this problem ?

  9. Jim McKeeth says:

    @Waki: Set the ClientDataSet to active at runtime (in code).

  10. Kim Jensen says:

    I have problem getting the data I can’t find out what to set the root element to.
    my json starts like this:
    {“d”:{“results”:[{“__Metadata”:{“uri”:etc…},”Field1″:”data”,”Field2″:”data”,

  11. Jim McKeeth says:

    @Kim: Not all JSON documents are formatted to work with the current TRESTClient implementation. You will need to experiment with your JSON document and see if you find one that works. Also check out the JSONDebugger that ships with XE5 for a little more flexibility.

  12. Kim Jensen says:

    I have used Delphi since version 1, for two years ago I start using Eclipse to write application for Android. I’m now looking if I should use Delphi XE5 for my Android application and until now I have not been able to make an application easier in Delphi then using Eclipse, most of the test I have made does not look promising for Delphi I always run into a road block. In Eclipse I can use GSon to read a json just by selecting the Root Element “results”, no debugger needed.Look like I have to wait for XE6 which should be out next year.

  13. Pingback: Livebinding JSON objects and arraysQueryBy | QueryBy, ejjuit, query, query by, queryby.com, android doubt, ios question, sql query, sqlite query, nodejsquery, dns query, update query, insert query, kony, mobilesecurity, postquery, queryposts.com, sapquery

  14. RJ says:

    I am having the same problem as Chatchai. I want to send a customer list from rest server and use Trestclient to read it into table format. Seems like whatever I do I get an extra set of outer square brackets [] and the REST debugger gets no tabular data. Is there a server side XE 5 Delphi example out there for sending a table as JSON.

  15. RJ says:

    Could also just be the same problem as Kim Jensen. I can’t seem to set any root element other than ‘result’

  16. RJ says:

    function TServerMethods1.GetList1:string;
    var
    ja:TJsonArray;
    j1:TJsonObject;
    j2:TJsonObject;
    //j3:TJsonObject;
    begin
    ja := TJsonArray.Create;
    j1 := TJsonObject.Create;
    j2 := TJsonObject.Create;
    j1.AddPair(‘Lastname’,’Doe’);
    j1.AddPair(‘Firstname’,’John’);
    ja.AddElement(j1);
    j2.AddPair(‘Lastname’,’Jane’);
    j2.AddPair(‘Firstname’,’Mary’);
    ja.AddElement(j2);

    GetInvocationMetadata().ResponseCode := 200;
    GetInvocationMetadata().ResponseContent := ja.ToString;
    end;

  17. RJ says:

    This works but in my humble opinion filtering by result should not send the outer brackets associated with the result package. Result should give you the array which you sent so that you may work with it.

  18. Abhinav Sanghvi says:

    I am using super object to read JSON, but it is easily happening in fire monkey form application, but it is throwing error of windows and 2154 in fire monkey mobile form app for IOS, How can I resolve it ?

  19. Jim McKeeth says:

    I’ve not used SuperObject. I’d suggest checking their support channels.

  20. Abhinav Sanghvi says:

    When on this upcoming json
    { “ApplicationHierarchyPosition”: “IOGLO”,
    “DisplayName”: “Rahul Kag”,
    “FirstLogin”: false,
    “HierarchyPosition”: “IOGLO0000300009000040003J”,
    “Id”: “790488700”,
    “InterfaceDomain”: “dev.LiveStuff.com”,
    “LoginEntityHierarchyPosition”: “IOGLO00003000090000100005”,
    “LoginHierarchyPosition”: “IOGLO0000300009000040003J”,
    “LOSID”: “WTEG1laIbSgigo2WWjSn0OjAEOO8fVjXTMgnaIIjhZexEtb3SW3vWpkSr6goNpsLcDFKyBruQQPJhiuXtR4q56Jw2cz8rzPTje\/j3EG0JI5gCui7eNiRMOFEYt\/qt4L1qheTwxGwMWxwam35li7Qygq6HNXsbqhBOmSSMCFTSBs=”,
    “NamePath”: “[Application]\\\\Structure\\\\Organization\\\\Users\\\\Rahul Kag”,
    “ObjectType”: “Administrator”,
    “ObjectTypeName”: “Administrator”,
    “PointerType”: “O”,
    “PrimaryObjectTypePosition”: “IOGLO000010000100008000070000100001”,
    “System”: “IOGLO000030000900001000050000R00004”,
    “SystemHierarchyPosition”: “IOGLO000030000900001000050000B00009”,
    “UserInterfaceNamePath”: “[Application]\\\\Structure\\\\Assets\\\\User Interfaces\\\\Standard”,
    “UserName”: “rkag@livetechnology.com”
    }

    when I am trying to parse this data Through JSONObject and JSONPair
    so this is returning this expected HPOS……

    “HierarchyPosition”:”IOGLO0000300009000040003J”
    HierarchyPosition
    IOGLO0000300009000040003J

    but when we are applying the same thing on This data it is not returning the expected data…with help of JSONObject, JSONPair and JSONArray

    [{
    “DisplayName”: “My Lists”,
    “Formatted Name”: “My Lists”,
    “HierarchyPosition”: “IOGLO0000300009000040003J0000600001”,
    “Id”: “805786106”,
    “LevelNumber”: 7,
    “NamePath”: “[Application]\\\\Structure\\\\Organization\\\\Users\\\\Rahul Kag\\\\LiveStuff\\\\My Lists”,
    “NodeLastUpdate”: “41618.3662842593”,
    “ObjectType”: “3AC3BAD2-B9FD-4B20-BC24-DD2956BFE8CD”,
    “ObjectTypeName”: “My Lists”,
    “PointerType”: “O”,
    “Previewable”: false,
    “PrimaryObjectTypePosition”: “IOGLO0000100001000020000F00002”,
    “Rank”: 1,
    “__TOTALITEMS__”: 5
    },
    {
    “DisplayName”: “My Stuff”,
    “Formatted Name”: “My Stuff”,
    “HierarchyPosition”: “IOGLO0000300009000040003J0000600002”,
    “Id”: “805786107”,
    “LevelNumber”: 7,
    “NamePath”: “[Application]\\\\Structure\\\\Organization\\\\Users\\\\Rahul Kag\\\\LiveStuff\\\\My Stuff”,
    “NodeLastUpdate”: “41646.1457873843”,
    “ObjectType”: “C012ABFF-FD69-4706-9D13-A6A933403999”,
    “ObjectTypeName”: “My Stuff”,
    “PointerType”: “O”,
    “Previewable”: false,
    “PrimaryObjectTypePosition”: “IOGLO0000100001000020000F00001”,
    “Rank”: 2
    }]

  21. Pingback: Delphi XE5 Mobile REST Client Demo | The Podcast at Delphi.org

  22. Roberto says:

    Do you have to send the source code of DataSnap Server?

  23. Jim McKeeth says:

    Not sure what you mean.

  24. Roberto says:

    Your mobile app is consuming methods of a DataSnap server correct?
    You have the source code of the DataSnap Server methods to download?
    I’m sorry I can not speak English, I’m using google translator.

  25. Roberto says:

    I’m sorry now that I saw that it comes to a web server.
    Congratulations your code is very nice and helped me a lot.

  26. Jim McKeeth says:

    Correct, my demo isn’t using DataSnap. Glad it was helpful for you.

  27. Roberto says:

    Hello Jim!
    I really need your help.
    Not encounter any teaching materials to communicate with Android Json.
    I did download your program, but I have many difficulties to adapt to my DataSnap server Json.
    My server is already running and returning a list of products in Json.
    Could you help me in converting your project to this my server?
    The address of the method that returns Json is:
    http://189.124.68.57:81/Datasnap/Rest5.dll/Datasnap/Rest/Tsm/Banco
    I’m sorry, but in my language found nothing.

  28. Jim McKeeth says:

    You would want to use DataSnap to communicate to a DataSnap server. While DataSnap uses JSON and REST, the DataSnap client has all the features necessary to talk to a DataSnap server. Check out the DataSnap labs http://www.embarcadero.com/rad-in-action/delphi-labs and the Documentation on DataSnap http://docwiki.embarcadero.com/RADStudio/XE5/en/Developing_DataSnap_Applications

  29. Humberto says:

    Works perfect on Android but debugging it on iOS i get error “Project raised exception class Bad system call (12).” on REST.Client.pas line 2625:

    > FClient.HTTPClient.Post(LURL, LBodyStream, LResponseStream)

    Does anyone have the same issue? Even the simplest code is causing this error.

    Thanks in advance

  30. Jim McKeeth says:

    I just ran it on both my iPad 3 and iPhone simulator without trouble. Are you running Update 2? I’m not aware of any bugs like that in previous versions, but better safe than sorry.

  31. Humberto says:

    I was using a BaseUrl=”https//…”, now it is working after I changed it to “http//…”

  32. Jeff says:

    Hi Jim,

    thanks for your demo source, it works on my Android.
    I want to make an Android app that get data from php files and convert into JSON. I’m a newbie here and don’t know where to start. Do you have any tutorials to make it happen?

    thanks

  33. joshua1972 says:

    Hello Jim, Nice example!
    I ask you: can i send a JSONObject to a Method on a DataSnap Server via RESTClient ? Or I can only send simple types such as string and integer? If I can, how do I?

  34. Uli Becker says:

    Hi Jim,

    thanks a lot for this helpful sample.

    Maybe you’ll know a workaround for that (in the Embarcadero NG nobody can help): if there is a string with a size of > 255 in the JSON, the databaseadapter creates a field of type ftString in the ClientDataset which is limited to 255. That’s why the whole thing isn’t useable any more if you have such data.
    Any help would be very much appreciated.

    Regards Uli Becker

  35. @Jim, TRestClient not recognize any JSON format? is not JSON a standard? Im having troubles with a JSON server also, don’t understand why TRestClient don’t understand any JSON structure?

  36. Jim McKeeth says:

    Verify that it is actually JSON. It might be non-standard JSON.

  37. Abhinav Sanghvi says:

    Hi David,
    Will you please answer me about this ……

    With help of ClientDataSet and Rest(RestClient, RestRequest, RestResponse and RestResponseDataSetAdapter),
    I am able to insert items in ListView through Live Binding, But when next JSON (or any other rest request is called and try to insert it’s items into ListView through LiveBinding all old items of ListView get removed and all new ones are updated)
    is called and new items are inserting then all old items get removed, So Can’t we keep old items too and can also add new items through Live Binding for completion of list, In Delphi XE5 for FireMonkey Mobile ?

    I want to know one more thing is that….
    as we can supply RestResponse to the RestResponseDataSetAdapter.Response,
    so at place of giving response, can we pass JSON or after converting JSON as RestResponse, we can supply , or Not ?
    and if yes so How can we…. ?

    Any help will be greatly appreciable.

  38. Jim McKeeth says:

    When you run the RESTRequest again you are replacing the existing contents of the ClientDataSet. Instead two multiple FDMemTables: One attached to the Adapter, the other holds new records. Then use an TFDLocalSQL to select all rows and columns from both, and use live bindings on that DataSet.

  39. Abhinav Sanghvi says:

    Hey David,
    Thanks but I am not getting exactly what I need, I am trying with FDMemTable but not getting exactly and I don’t have any deep idea about it and FDLocalSQL,I am new to it, Have you any related tutorial to it and
    Please have look at it also
    I am trying to bind JSON responses into one JSONArray but, JSONArray is accepting only firstJSON’s objects

  40. Pingback: Source Program In Delphi

Leave a Reply