Proper way to Use Xamarin Forms with RESTFull Web Services and backend data

ahmedkhanahmedkhan USUniversity ✭✭

Hi, I'm new to Xamarin.Forms,

I have some questions about project structure and data access. I would like to create a Xamarin.Forms.Portable Solution for all three platforms with common data access code.

I'm trying to access a simple web based service with a REST endpoint without having to code that data access 3 times for it in each platform's OS specific project.

Here's what I would like to do in the common PCL project of my Xamarin.Forms solution (or shared project)

  1. use RESTSharp client or other REST Client to read a REST Service for JSON data (any examples available for this?)
  2. using JSON.NET to create business objects from that data (not sure what components or clients are available for this it doesnt seem like you can use xamarin components in the common PCL project but it appears you can use nuget packages)
  3. populate the a SQLite Database with that data (I see documentation on how to do this)
  4. create Xamarin.Forms pages with that data for each platform (I see docs and understand how to do this)
  5. Post updates back to the backend Service (best practices for this?)

I'm a bit confused about Steps 1 and 2 in the context of Xamarin.Forms - what's the best methodology for querying web services in Xamarin.Forms Solutions? Also is there a simple cross platform app that exemplifies this structure available?

thanks!

Posts

  • CraigDunnCraigDunn USXamarin Team Xamurai

    This simple HttpClientDemp shows how to download data directly using HttpClient and parse it using Json.net.

    It only takes a few lines of code, as long as you've set-up the object model to match the Json response. That sample then binds the Json result to a ListView.

    There are also examples using Azure and Parse.

  • PaulPPaulP USMember ✭✭

    @ahmedkhan‌ Here is an example of a full app

    https://github.com/paulpatarinski/ShouldIWashMyCar
    

    You can find examples of consuming Web Services here :

    https://github.com/paulpatarinski/ShouldIWashMyCar/tree/master/Core/Services
    

    The calling and parsing is done in these extension methods :

    https://github.com/paulpatarinski/ShouldIWashMyCar/blob/master/Core/Helpers/HttpClientExtensions.cs
    

    This should help answer 1, 2 and 4.

    Cheers,
    Paul

  • ahmedkhanahmedkhan USUniversity ✭✭

    Thanks Paul & Craig!, those examples are what I was looking for and will help immensely.

    As Enterprise developers, Xamarin.Forms is particulary exciting since it offers the ability to centralize CRUD and service access in cross platform PCL and then allow for a simple MVVM based common UI for the holy grail: a cross platfrom C# based native app- Since a lot of Enterprise apps are simple crud apps that are not dependant on flashy UI capability but simple lists and forms, this makes it appealing for those developers. However these developers are usually coming from the windows desktop world so PCLs and mobile OS specific requirements are all somewhat new -THEY NEED GUIDANCE- It would be great if the examples presented would be more in line with enterprise needs -e.g. things like Authentication, REST service access, OAuth, CRUD and ORM capabilities More examples of Dependency Injection, in general more enterprise examples that showcase plumbing along with UI :)

    I think one of the great benefits of the base Xamarin platform is the ablitiy to use components from the component store - It would be great if Xamarin could provide more examples and a clear demarcation of the capabilities of Shared Project or PCL - For example a flowchart or documentation that explains how to setup a solution for various needs e.g. Authentication, CRUD. Device hardware etc.. I realize that such examples are available amd growing, but they are scattered at present..

    Right now I've been looking for a list of available PCLs and what platforms are supported including what NUGET packages are Xamarin friendly -it would be great if Xamarin setup a list of supported PCLs -e.g. something like this:
    http://www.hanselman.com/blog/CrossPlatformPortableClassLibrariesWithNETAreHappening.aspx

    Thanks!

  • CraigDunnCraigDunn USXamarin Team Xamurai

    Hey @ahmedkhan‌, Shared Projects and PCLs are covered in our general documentation. They each have their own pros & cons related to cross-platform development that apply whether you are using Xamarin.Forms or not.

    The only additional point I'd add is that Xaml for Xamarin.Forms currently only works with PCL on Mac OS X / Xamarin Studio.

    The choice between them doesn't otherwise limit what you can build - it really comes down to whether you prefer "dependency injection" or #ifdef compiler directives as a way to separate your platform-specific code.

  • g3r4g3r4 MXMember, University

    Hi @CraigDunn‌ !
    I am actually doing something similar to what ahmedkhan says, the only thing is that I started building the application using Xaml on Xamarin Studio for Mac, I have a PCL project for code sharing, and have some "skelleton pages" already built with Xaml. The problem I have now is that I can't create an HttpClient or RestSharp client or any client to get a json from a web service in my Xamarin.Forms code. And for what I am reading on your response, this is impossible at the moment, am I right?

    Do you have any ideas that could help me solve this ?

    Thanks !

  • CraigDunnCraigDunn USXamarin Team Xamurai

    You can also use HttpClient (from the Microsoft.Net.Http NuGet) as shown in the HttpClient sample.

  • vinoth.3237vinoth.3237 USMember

    Hi @CraigDunn‌

    I too actually going on with the same as ahmed khan I would like to do in the common PCL project of my Xamarin.Forms solution (or shared project) in visual studio to share single reusable code for various platform.

  • PD.6026PD.6026 USMember ✭✭

    Hi @CraigDunn‌ , I am facing a problem regenerating the HttpClientDemo app. After button click, the data is displayed, but the app is crashing after some time with Unhandled Exception error
    "System.ArgumentNullException: Argument cannot be null."

    When debugging the code, var response is returning null value in GeoNamesWebService class. Also the debugger is jumping out of function after response statement. Can you please help as I am new with webservice and developing an app using this method and having the same problem.

  • vinguanvinguan USMember ✭✭
    edited November 2015

    RestShap has a portable branch but is not stable as the master branch. You could use my library https://github.com/vinguan/restion. It is pretty awesome.

  • alarghialarghi ARMember

    Check my Request class http client, It may help you https://github.com/4gus71n/Xamarin.Droid

  • HistoryaHistorya USMember ✭✭✭

    any idea about how to consume an existing ws for login form?

Sign In or Register to comment.