1 // create an HTTP client
2 httpclient client = new defaulthttpclient ();
3 // create a GET request
4 httpget = newhttpget ("http://www.store.com/products ");
5 // send a request to the server and obtain the results returned by the server
6 httpresponse response=client.exe cute (httpget );
7 // The returned results may be placed in inputstream, And the HTTP header is medium.
8 inputstream = response. getentity (). getcontent ();
9 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.
01 // create an HTTP client
02 httpclient client = new defaulthttpclient ();
03 // create a POST request
04 httppost = newhttppost ("http://www.store.com/product ");
05 // put the assembled data in httpentity and send it to the server
06 final list datalist = new arraylist ();
07 datalist. Add (New basicnamevaluepair ("productname", "cat "));
08 datalist. Add (New basicnamevaluepair ("price", "14.87 "));
09 httpentity entity = new urlencodedformentity (datalist, "UTF-8 ");
10 httppost. setentity (entity );
11 // send a POST request to the server and obtain the results returned by the server. This may be because the product ID or failure information is added successfully.
12 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.
01 // create an HTTP client
02 httpclient client = new defaulthttpclient ();
03 // create a put request
04 httpput = new httpput ("http://www.store.com/product/1234 ");
05 // put the assembled data in httpentity and send it to the server
06 final list datalist = new arraylist ();
07 datalist. Add (New basicnamevaluepair ("price", "11.99 "));
08 httpentity entity = new urlencodedformentity (datalist, "UTF-8 ");
09 httpput. setentity (entity );
10 // send a put request to the server and obtain the results returned by the server. The modification may be successful or fail.
11 httpresponse response=client.exe cute (httpput );
Step 4: Delete the added item. You only need to send a Delete request to the server.
1 // create an HTTP client
2 httpclient client = new defaulthttpclient ();
3 // create a Delete request
4 httpdelete = newhttpdelete ("http://www.store.com/product/1234 ");
5 // send a Delete request to the server and obtain the results returned by the server. The deletion may be successful or fail.
6 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.