REST service Introduction, rest Service
RESTful service is an architecture model that has become popular in recent years. Its lightweight web service uses the native http get, PUT, POST, and DELETE methods. Compared with the complex SOAP and XML-RPC, the REST mode Web Service is obviously more concise, and more web services begin to adopt the REST style design and implementation. For example, Amazon.com provides a REST-style Web Service for book search. Yahoo provides a REST-style Web service. REST is not always the right choice. It has become popular as a way to design Web Services, which relies less on proprietary middleware (such as an application server) than on SOAP and WSDL-based methods. In a sense, by emphasizing early Internet standards such as URI and HTTP, REST is a regression of the Web approach before the era of large application servers. Example:
Author: Petter Liu
Source: http://www.cnblogs.com/wintersun/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.
This article is also published in Petter Liu Blog, my independent Blog.
Differences between REST and HTTP
For example, if you want to get weather information, you cannot predict the weather, so you need to get it from the Meteorological Administration Server. The Meteorological Administration Server may be a WebService built using the Rest or other network service application frameworks, as long as you visit the website and send a message to the server, the server will return the corresponding information (the information here may be json or xml data, which needs to be parsed before use ), so that your program can also use more professional and authoritative data, and the transmission mode may be tcp or http. Rest applications are on the server, while http is the method for exchanging data between the server and the visitor.
How to use the Rest service on Android
Let's take the Rest service in the following network store as an example to see how we call it through the Android client (Here we only introduce how to send requests and get server responses ).
The Android Class Library provides everything we need.
The principle of Rest is to send GET, POST, PUT, and DELETE operations to a resource URI for obtaining, creating, saving, and deleting operations.
The first step is to see how to request all product information:
// Create an http client
HttpClient client = new DefaultHttpClient ();
// Create a GET request
HttpGet httpGet = new HttpGet ("www.store.com/products ");
// Send a request to the server and obtain the results returned by the server
HttpResponse response=client.exe cute (httpGet );
// The returned results may be placed in InputStream, And the http Header is medium.
InputStream inputStream = response. getEntity (). getContent ();
Header [] headers = response. getAllHeaders ();
By parsing the stream returned by the server, we can convert it into a string to obtain the corresponding data.
In the second step, you can add products to the server. In the same way, we can create a POST request with related product information.
// Create an http client
HttpClient client = new DefaultHttpClient ();
// Create a POST request
HttpPost httpPost = new HttpPost ("www.store.com/product ");
// Put the assembled data in HttpEntity and send it to the server
Final List dataList = new ArrayList ();
DataList. add (new BasicNameValuePair ("productName", "cat "));
DataList. add (new BasicNameValuePair ("price", "14.87 "));
HttpEntity entity = new UrlEncodedFormEntity (dataList, "UTF-8 ");
HttpPost. setEntity (entity );
// Send a POST request to the server and obtain the result returned by the server. It may be a successful increase in the returned product ID or failure information.
HttpResponse response=client.exe cute (httpPost );
Step 3: If you want to modify the product information, you only need to create a PUT request with the parameters to be modified. In this example, if the returned ID of the added product in step 3 is 1234, the price of the product will be changed to 11.99.
/... The remaining full text>