文档章节

Android Restful Webservice Tutorial– 1

JayPark不作死
 JayPark不作死
发布于 2014/08/18 22:42
字数 908
阅读 268
收藏 0

Here in Android Tutorial Blog, we have already discussed lot about using Webservice in Android applications. Take a look at Android Webservice Tutorial to know list of articles we wrote on webservice.

Integrating Android application and Webservice is not that much difficult. We can make it work if we know basics of webservice and the procedure to make webservice call from inside Android applications.

In this post, I will be discussing about creating and invoking RESTful webservice in Android applications.

This tutorial will be published as three parts:

Introduction to RESTful webservice

What is RESTtful webservice?

REST describes a set of architectural principles by which data can be transmitted over a standardized interface (such as HTTP). The acronym REST stands for Representational State Transfer, this basically means that each unique URL is a representation of some object.

REST asks developers to use HTTP methods explicitly and in a way that’s consistent with the protocol definition. This basic REST design principle establishes a one-to-one mapping between create, read, update, and delete (CRUD) operations and HTTP methods. According to this mapping:

To create a resource on the server, use POST.
To retrieve a resource, use GET.
To change the state of a resource or to update it, use PUT.
To remove or delete a resource, use DELETE.

For example:

http://weatherinfo.org/getweather/mumbai – It gets the weather information of Mumbai (one of the Indian cities)
http://employer.com/update/salary/130/3000 – It updates Salary of Employee with Employee ID ’130′ as $3000.
http://countries.com/add/singapore – It adds country ‘Singapore’ to the list of Countries
http://product.com/delete/dell/inspiron15r – It deletes Dell Inspiron 15R from the list of products

[Domain names mentioned above are just for examples, they are not working domains]

Who is using Restful webservice?

Internet giants like Yahoo, Amazon, eBay and more are using Restful webservice

Rest vs SOAP

REST SOAP
REST stands for REpresentational State Transfer SOAP stands for Simple Object Access Protocol
REST is not XML protocol based SOAP is a XML based messaging protocol
REST doesn’t have any specification SOAP has specifications like WSDL etc
REST doesn’t enforce message format as XML or JSON SOAP enforces message format as XML
Light weight – due to the usage of JSON Heavy weight – due to the usage of XML
Easy to parse the response Bit difficult to parse the response

RESTFul Webservice Request and Response – Drill down

Fist step in designing RESTFul webservice is choosing the right domain name – say weatherinfo.org to retrieve weather information of cities.

Let us take the first example (weatherinfo.org) which takes city name as input, composes weather information of the city and respond back to the browser. Assume the response is in XML format, this can be in other formats like JSON as well.

Here is the structure of Request and Response:

HTTP Request
The client request from the browser will look like:

1

HTTP Response
The server response will look like

1
2
3
4
5
6
7
8
9
HTTP/1.1 200 Ok
Date: Mon, 14 Apr 2014 10:20:58 GMT
Content-Type: text/xml
Content-length: 139
 
< City name = "Mumbai" datetime = "2014-04-14 10:20:58 GMT" >
< Condition >Scattered Clouds</ Condition >
< Temp >33</ Temp >
</ City >

Line 1 is the initial line which has the HTTP response code – 200 OK, lines 2 through 4 are the HTTP headers (there can be lots of headers, only 3 are shown here), line 5 is the mandatory blank line separating header and body, and lines 6 through 10 constitute the “HTTP Body (or content)” – this part is the data that the response carries and can be in any format, not necessarily XML.

In fact, the most commonly used format on the web is HTML – one that web servers use to send back data to browsers. Whatever it is, the “Content-type” header usually specifies it. But if you are writing a web service, JSON is a better choice, but that is upto me. If your web service does not return complex or composite data, the format does not need to be JSON – it can be plain , in which case the body will just be a string of characters.

Advantages of using RESTful webservice

  1. RESTful Web services are designed with less dependence on proprietary middleware (for example, an application server) than the SOAP- and WSDL-based kind.

  2. As per the RESTful interface design, XML or JSON over HTTP is a powerful interface that allows internal applications, such as Asynchronous JavaScript + XML/JSON (Ajax)-based custom user interfaces, to easily connect, address, and consume resources.

  3. The great fit between Ajax and REST has increased the amount of attention REST is getting these days.

  4. Exposing a system’s resources through a RESTful API is a flexible way to provide different kinds of applications with data formatted in a standard way. It helps to meet integration requirements that are critical to building systems where data can be easily combined (mashups) and to extend or build on a set of base, RESTful services into something much bigger.

本文转载自:http://programmerguru.com/android-tutorial/android-restful-webservice-tutorial-part-1/

JayPark不作死
粉丝 15
博文 51
码字总数 13622
作品 0
苏州
程序员
私信 提问
C# Web Service 学习

Your first C# Web Service http://www.codeproject.com/Articles/863/Your-first-C-Web-Service Everything About REST Web Services - What and How - Part 1 http://www.codeproject.com/......

lgxn
2014/03/23
59
0
Android Restful Webservice Tutorial – 2

How to create RESTFul webservice in Java? In this post, I will be discussing about creating RESTful webservice in Java and in the next post will be discussing how to consume RES......

JayPark不作死
2014/08/18
87
0
为开发人员准备的 10 个 Android 教程

1. OpenGL ES 2.0 该教程为你展示如何使用 OpenGL ES 2.0 API 开发简单的 Android 应用,执行一些基本的图形操作。 2. Android Development Tutorial 该教程展示使用Eclipse开发Android应用,...

鉴客
2011/09/21
2.1K
6
2014 非常好用的开源 Android 测试工具

当前有很大的趋势是转向移动应用平台,Android 是最广泛使用的移动操作系统,2014 年大约占 80% 以上的市场。在开发 Android 应用的时候要进行测试,现在市场上有大量的测试工具。 本文主要是...

oschina
2014/10/15
39.3K
11
在Android中调用C#写的WebService

由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章。在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebService当做一个对象使...

guorongjin
2015/12/30
3
0

没有更多内容

加载失败,请刷新页面

加载更多

分布式协调服务zookeeper

ps.本文为《从Paxos到Zookeeper 分布式一致性原理与实践》笔记之一 ZooKeeper ZooKeeper曾是Apache Hadoop的一个子项目,是一个典型的分布式数据一致性的解决方案,分布式应用程序可以基于它...

ls_cherish
今天
4
0
redis 学习2

网站 启动 服务端 启动redis 服务端 在redis 安装目录下 src 里面 ./redis-server & 可以指定 配置文件或者端口 客户端 在 redis 的安装目录里面的 src 里面 ./redis-cli 可以指定 指定 连接...

之渊
昨天
2
0
Spring boot 静态资源访问

0. 两个配置 spring.mvc.static-path-patternspring.resources.static-locations 1. application中需要先行的两个配置项 1.1 spring.mvc.static-path-pattern 这个配置项是告诉springboo......

moon888
昨天
4
0
hash slot(虚拟桶)

在分布式集群中,如何保证相同请求落到相同的机器上,并且后面的集群机器可以尽可能的均分请求,并且当扩容或down机的情况下能对原有集群影响最小。 round robin算法:是把数据mod后直接映射...

李朝强
昨天
4
0
Kafka 原理和实战

本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/bV8AhqAjQp4a_iXRfobkCQ 作者简介:郑志彬,毕业于华南理工大学计算机科学与技术(双语班)。先后从事过电子商务、开放平...

vivo互联网技术
昨天
24
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部