Android uses http get/post to submit data to the server

Source: Internet
Author: User
1. Get Method
  • Method: add the corresponding data after the URL, such as http: // 172.22.35.112: 8080/videonews/getinfoservlet? Title = Hobbit & timelength = 100;
  • Disadvantage: the data submitted in get mode can only send less than 2 K of data, which is suitable for sending small-capacity data. In addition, if the data sent is in Chinese, you need to perform garbled processing on the URL and the server (set the encoding mode to display Chinese characters). Otherwise, garbled characters may occur. The processing method is as follows:

 

2. Post Method

  • Method:
  1. The request parameter group is used to construct object data, that is, the Data Group is combined in a certain format. For example, Title = title = hobbites & timelength = 100;
  2. Obtain the byte data of the object data, such as byte [] entry = data. deletecharat (data. Length ()-1). tostring (). getbytes ();
  3. Create an httpurlconnection and set the connection.
  4. Set the header fields of the HTTP request. If no cookie is used, some header fields can be omitted or not set, but content_type and content_length must be set. For example:
  5. Since the object data is sent from the client to the server, the output stream can be obtained and then written outside the output stream (Note: Data is written only when the server response code is obtained. From this we can see that the object data is converted to byte bytes for later transmission,Code.). For example:

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.