XAMARIN FORMS
NOOB TO MASTER
By Rendy Del Rosario and Charlin Agramonte
Week 5
Objectives
- Understand what is a Web service
- Understand what is a REST API and how to use in Xamarin
- Consume a real API in Xamarin
Web Service
A web service is a collection of open protocols and standards used for exchanging
data between applications or systems.
For transferring data it uses:
- XML
- JSON
SOAP RESTFUL
TYPES
API (Application Programming Interface)
Is a set of defined functions and methods for interfacing with the underlying
operating system or another program or service running on the computer.
RESTful Web Service
What are REST services?
REST (Representational State Transfer) is an architecture for creating distributed
applications which is modeled around the HTTP specification
Definition
URL HTTP methods
A media type for the
data
https://www.some_address.com/customers GET/POST/PUT/PATCH/DELETE JSON/ XML
Rest operations
- Get
- Post
- Put
- Patch
- Delete
Rest operations: GET
This operation is used to retrieve data from the web service.
https://www.some_address.com/users/
https://www.some_address.com/users/5 https://www.some_address.com/users?id=5
Rest operations: POST
This operation is used to create a new item of data on the web service.
https://www.some_address.com/users/5
Rest operations: PUT
This operation is used to update an item of data on the web service.
https://www.some_address.com/users/5
Rest operations: PATCH
This operation is used to update an item of data on the web service by describing
a set of instructions about how the item should be modified.
https://www.some_address.com/users/5
Rest operations: DELETE
This operation is used to delete an item of data on the web service.
https://www.some_address.com/users/5
Media Types
JSON
JSON (JavaScript Object Notation) is a
lightweight format that is used for data
interchanging. It is based on a subset of
JavaScript language (the way objects are
built in JavaScript).
HttpStatus
The REST service sends an HTTP status code in the
HttpResponseMessage.StatusCode property, to indicate whether the HTTP
request succeeded or failed. The common responses for this operation are:
201 (CREATED)
400 (BAD
REQUEST)
500 (SERVER
ERROR)
404 (NOT FOUND)
Example
Security in REST
- Security is ultimately decided by the service
– the client can only conform to what the
service allows
- Should always prefer https to protect the
data peer-to-peer
REST in Xamarin
Xamarin API Options
HttpClient ServiceStack RestSharp Platform Specific
Most common
approach, built into
.NET
Full fledged 3rd party
web services
framework, has client
PCL for consuming
REST services
Full 3rd party client
library that supports
file downloads,
authentication,
chunking, etc
Native Platform Apis
for networking calls
HttpClient
The HttpClient class is used to send and receive requests over HTTP. It
provides functionality for sending HTTP requests and receiving HTTP
responses from a URI identified resource. Each request is sent as an
asynchronous operation.
HttpClient - Get
JsonConvert is a Json.NET class that can serialize and
deserialize data from a JSON string or stream based on a
specified Type
Deserialize object
JsonConvert.DeserializeObject<ObjectoToStrealize>(text);
HttpClient - Post/Put/Delete
Must serialize body and include encoding and content type
HttpClient customizations
Let’s do it step by step
1-Create an Interface IApiService for your call definitions
2-Create a class ApiService to handle your requests and implement the
previously created interface
3-Implement your request calls in your Api Service and define them in your
Interface
4-Do your calls in your ViewModel
5-Show the data in your views
GROUP EXERCISE
Interact with our first API
INDIVIDUAL EXERCISE
Using the Public API
Create an app for get a RNC of a company
TIME: 45 MINUTES
IOS - NSAppTransportSecurity
App Transport Security (ATS) enforces
secure connections between internet
resources (such as the app's back-end
server) and your app.
Mobile Networks
Applications should always determine whether a network is available
before starting a network operation
INDIVIDUAL EXERCISE
Add connectivity handler to your actual project
TIME: 15 MINUTES
Refit
Refit is a library heavily inspired by Square's Retrofit library, and it turns
your REST API into a live interface
https://github.com/paulcbetts/refit
Helpful tools for Json and Request Handlers
- http://json2csharp.com/
- https://www.getpostman.com/docs/postman/launching_postman/navi
gating_postman
HOMEWORK PRACTICE
Using the Public API for Weather
Create an App to get the Weather of Santo Domingo, DR
Recommended Lectures
- Web Service
https://developer.xamarin.com/guides/xamarin-forms/cloud-services/consuming/
- HttpClient in Xamarin Forms
http://blog.xhackers.co/httpclient-with-xamarin-forms/
https://www.youtube.com/watch?v=xNP-K37mssA
By Rendy Del Rosario and Charlin Agramonte
Week 5
THANK YOU!!!

Xamarin Workshop Noob to Master – Week 5

  • 1.
    XAMARIN FORMS NOOB TOMASTER By Rendy Del Rosario and Charlin Agramonte Week 5
  • 2.
    Objectives - Understand whatis a Web service - Understand what is a REST API and how to use in Xamarin - Consume a real API in Xamarin
  • 3.
    Web Service A webservice is a collection of open protocols and standards used for exchanging data between applications or systems. For transferring data it uses: - XML - JSON SOAP RESTFUL TYPES
  • 4.
    API (Application ProgrammingInterface) Is a set of defined functions and methods for interfacing with the underlying operating system or another program or service running on the computer.
  • 5.
  • 6.
    What are RESTservices? REST (Representational State Transfer) is an architecture for creating distributed applications which is modeled around the HTTP specification
  • 7.
    Definition URL HTTP methods Amedia type for the data https://www.some_address.com/customers GET/POST/PUT/PATCH/DELETE JSON/ XML
  • 8.
    Rest operations - Get -Post - Put - Patch - Delete
  • 9.
    Rest operations: GET Thisoperation is used to retrieve data from the web service. https://www.some_address.com/users/ https://www.some_address.com/users/5 https://www.some_address.com/users?id=5
  • 10.
    Rest operations: POST Thisoperation is used to create a new item of data on the web service. https://www.some_address.com/users/5
  • 11.
    Rest operations: PUT Thisoperation is used to update an item of data on the web service. https://www.some_address.com/users/5
  • 12.
    Rest operations: PATCH Thisoperation is used to update an item of data on the web service by describing a set of instructions about how the item should be modified. https://www.some_address.com/users/5
  • 13.
    Rest operations: DELETE Thisoperation is used to delete an item of data on the web service. https://www.some_address.com/users/5
  • 14.
  • 15.
    JSON JSON (JavaScript ObjectNotation) is a lightweight format that is used for data interchanging. It is based on a subset of JavaScript language (the way objects are built in JavaScript).
  • 16.
    HttpStatus The REST servicesends an HTTP status code in the HttpResponseMessage.StatusCode property, to indicate whether the HTTP request succeeded or failed. The common responses for this operation are: 201 (CREATED) 400 (BAD REQUEST) 500 (SERVER ERROR) 404 (NOT FOUND)
  • 17.
  • 18.
    Security in REST -Security is ultimately decided by the service – the client can only conform to what the service allows - Should always prefer https to protect the data peer-to-peer
  • 19.
  • 20.
    Xamarin API Options HttpClientServiceStack RestSharp Platform Specific Most common approach, built into .NET Full fledged 3rd party web services framework, has client PCL for consuming REST services Full 3rd party client library that supports file downloads, authentication, chunking, etc Native Platform Apis for networking calls
  • 21.
    HttpClient The HttpClient classis used to send and receive requests over HTTP. It provides functionality for sending HTTP requests and receiving HTTP responses from a URI identified resource. Each request is sent as an asynchronous operation.
  • 22.
    HttpClient - Get JsonConvertis a Json.NET class that can serialize and deserialize data from a JSON string or stream based on a specified Type
  • 23.
  • 24.
    HttpClient - Post/Put/Delete Mustserialize body and include encoding and content type
  • 25.
  • 26.
    Let’s do itstep by step 1-Create an Interface IApiService for your call definitions 2-Create a class ApiService to handle your requests and implement the previously created interface 3-Implement your request calls in your Api Service and define them in your Interface 4-Do your calls in your ViewModel 5-Show the data in your views
  • 27.
  • 28.
    INDIVIDUAL EXERCISE Using thePublic API Create an app for get a RNC of a company TIME: 45 MINUTES
  • 29.
    IOS - NSAppTransportSecurity AppTransport Security (ATS) enforces secure connections between internet resources (such as the app's back-end server) and your app.
  • 30.
    Mobile Networks Applications shouldalways determine whether a network is available before starting a network operation
  • 31.
    INDIVIDUAL EXERCISE Add connectivityhandler to your actual project TIME: 15 MINUTES
  • 32.
    Refit Refit is alibrary heavily inspired by Square's Retrofit library, and it turns your REST API into a live interface https://github.com/paulcbetts/refit
  • 33.
    Helpful tools forJson and Request Handlers - http://json2csharp.com/ - https://www.getpostman.com/docs/postman/launching_postman/navi gating_postman
  • 34.
    HOMEWORK PRACTICE Using thePublic API for Weather Create an App to get the Weather of Santo Domingo, DR
  • 35.
    Recommended Lectures - WebService https://developer.xamarin.com/guides/xamarin-forms/cloud-services/consuming/ - HttpClient in Xamarin Forms http://blog.xhackers.co/httpclient-with-xamarin-forms/ https://www.youtube.com/watch?v=xNP-K37mssA
  • 36.
    By Rendy DelRosario and Charlin Agramonte Week 5 THANK YOU!!!