Interaction between Android client and server

Source: Internet
Author: User

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.

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.