Cosc 54730 REST services What is REST REST

  • Slides: 15
Download presentation
Cosc 5/4730 REST services

Cosc 5/4730 REST services

What is REST? • REST stands for Representational State Transfer. • It is sometimes

What is REST? • REST stands for Representational State Transfer. • It is sometimes spelled "Re. ST". – It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used. – The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.

What is REST? (2) • RESTful applications use HTTP requests to post data (create

What is REST? (2) • RESTful applications use HTTP requests to post data (create and/or update), read data (e. g. , make queries), and delete data. Thus, REST uses HTTP(s) for all four CRUD (Create/Read/Update/Delete) operations. – Sound familiar? • Think Content. Provider! – The returned data maybe some combination of HTML, XML, JSON, etc. Up to the API • There really is no standard.

What is REST? (2) • The web services may or may not require authentication.

What is REST? (2) • The web services may or may not require authentication. – For security, username/password tokens are often used. – For encryption, REST can be used on top of HTTPS (secure sockets). – Again, depends on the app.

Real world examples • Google’s Mirror API is a REST API • Twitter has

Real world examples • Google’s Mirror API is a REST API • Twitter has a REST API – This was their original API and, so far as I can tell, it's still the main API used by Twitter application developers), • Flickr • Amazon. com offer several REST services, e. g. , for their S 3 storage solution • Atom is a RESTful alternative to RSS,

Standards (not really) • There is only really one standard. It uses a webserver

Standards (not really) • There is only really one standard. It uses a webserver – The request may look like this • http: //somewhere. com/posts for all the posts – http: //somwhere. com/posts/1 for the first post. • http: //somwhere. com/query. php? data=posts for all posts – http: //somwhere. com/posts. php? data=posts&num=1 for the first post – The data is returned as csv file, json, xml, or other format.

JSON android • Android has built in methods for JSON • Import org. json

JSON android • Android has built in methods for JSON • Import org. json – Data is started in a Key value format in a JSONObject – JSONObject which can be a mix of JSONObjects and JSONArrays – JSONArray which is an array of JSONobjects – A good simple tutorial is here: • http: //www. tutorialspoint. com/android_json_parser. htm

REST and JSON demo • Rest. Demo 1 – It shows basics of JSON

REST and JSON demo • Rest. Demo 1 – It shows basics of JSON – Uses a REST service designed for testing purposes • http: //jsonplaceholder. typicode. com/ – It loads the data via a Http. URLconnection (and Async. Task) – Display the data in recyclerview with a refreshlayout.

Creating your own. • Can be a simple or complex as you need –

Creating your own. • Can be a simple or complex as you need – Maybe combined with AJAX (via java script) calls as well – Think Google’s search that updates as you type. – The backend language is your choosing, but normally a script running via the webserver • It then access to a backend storage (ie file, database, etc) – At min it should have some way to query for information • Updating, deleting, and inserting data are based on need.

Re. ST Demo 2 • Uses php and database on a webserver. • The

Re. ST Demo 2 • Uses php and database on a webserver. • The query data is returned in a csv format. • Delete and update require an the use of the id value. – Insert just requires the other values. • Authentication is required – basic authentication via the web server

authenticator • Uses the java. net. Authenticator and java. net. Password. Authentication packages. •

authenticator • Uses the java. net. Authenticator and java. net. Password. Authentication packages. • In it’s simple form, you need to set the username and password for the authenticator Authenticator. set. Default(new Authenticator() { protected Password. Authentication get. Password. Authentication() { return new Password. Authentication(username, password. to. Char. Array()); }); • Where both username and password are Strings.

Post and parameters. • A get access looks like this: – http: //example. com/page?

Post and parameters. • A get access looks like this: – http: //example. com/page? param 1=value&parm 2=anothervalue • Where there are two parameters – param 1=value – Param 2=anothervalue • In a post method, it is in the request – It can be very complex for file uploads, but parameters can be done simply. – “param 1=value&parm 2=anothervalue” written out URL encoded UTF-8 format. – Then use the output. Stream to write to the web server. • See the get. Post. Data. String method is Rest. Demo 2.

Re. ST Demo 3 • It’s an attempt a system that displays pictures similar

Re. ST Demo 3 • It’s an attempt a system that displays pictures similar in concept to facebook, tumbler, and instragram. – It uses a async. Task to get the data for the recyclerview – And a second task to load the images as the cardview for it becomes visible.

References • • • http: //rest. elkstein. org/2008/02/what-is-rest. html https: //en. wikipedia. org/wiki/Representational_state_transfer http:

References • • • http: //rest. elkstein. org/2008/02/what-is-rest. html https: //en. wikipedia. org/wiki/Representational_state_transfer http: //developer. android. com/reference/org/json/package-summary. html http: //www. tutorialspoint. com/android_json_parser. htm http: //jsonplaceholder. typicode. com/ http: //stackoverflow. com/questions/29536233/deprecated-http-classes-android-lollipop-51 – For post parameters method. • http: //developer. android. com/reference/java/net/Http. URLConnection. html • http: //stackoverflow. com/questions/4883100/how-to-handle-http-authentication-usinghttpurlconnection • https: //en. wikipedia. org/wiki/Representational_state_transfer • http: //developer. android. com/training/basics/network-ops/connecting. html

Q&A

Q&A