How to use the rest service in Android to call WebService from the client

Source: Internet
Author: User


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.
 
 
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 first step is to see how to request all product information:
// Create an HTTP client
Httpclient client = new defaulthttpclient ();
// Create a GET request
Httpget = new httpget ("http://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 = 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 = new httppost ("http://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.

// Create an HTTP client
Httpclient client = new defaulthttpclient ();
// Create a put request
Httpput = new httpput ("http://www.store.com/product/1234 ");
// Put the assembled data in httpentity and send it to the server
Final list datalist = new arraylist ();
Datalist. Add (New basicnamevaluepair ("price", "11.99 "));
Httpentity entity = new urlencodedformentity (datalist, "UTF-8 ");
Httpput. setentity (entity );
// Send a put request to the server and obtain the results returned by the server. The modification may be successful or fail.
Httpresponse response=client.exe cute (httpput );
Step 4: Delete the added item. You only need to send a Delete request to the server.

// Create an HTTP client
Httpclient client = new defaulthttpclient ();
// Create a Delete request
Httpdelete = new httpdelete ("http://www.store.com/product/1234 ");
// Send a Delete request to the server and obtain the results returned by the server. The deletion may be successful or fail.
Httpresponse response=client.exe cute (httpdelete );
Now, it's so simple that you can call the rest service from the android client to add, delete, modify, and query resources.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.