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.