Use the httpurlconnection object to interact with the Internet

Source: Internet
Author: User
1. Obtain a webpage from the Internet
Send a request to read the webpage back as a stream.
1) create a URL object: URL url = new URL ("http://www.sohu.com ");
2) use the httpurlconnection object to obtain webpage data from the Network: httpurlconnection conn = (httpurlconnection) URL. openconnection ();
3) set connection Timeout: conn. setconnecttimeout (6*1000 );
4) determine the response code: If (conn. getresponsecode ()! = 200) throw new runtimeexception ("request URL failed ");
5) obtain the input stream returned by the Network: inputstream is = conn. getinputstream ();
6) string result = readdata (is, "GBK ");
Conn. Disconnect ();
Summary:
-- We must remember to set the connection timeout. If the network is poor, the android system will reclaim the resource interruption operation when the default time is exceeded.
-- The response code 200 returned is successful.
-- Use the bytearrayoutputstream class to write the input stream to the memory.
-- The operations on file streams in Android are the same as those in Java SE.

2. Get files from the Internet
Using the httpurlconnection object, we can obtain file data from the network.
1) create a URL object and pass in the file path: URL url = new URL ("http://photocdn.sohu.com/20100125/Img269812337.jpg ");
2) create an httpurlconnection object to obtain file data from the Network: httpurlconnection conn = (httpurlconnection) URL. openconnection ();
3) set connection Timeout: conn. setconnecttimeout (6*1000 );
4) determine the response code: If (conn. getresponsecode ()! = 200) throw new runtimeexception ("request URL failed ");
5) obtain the input stream returned by the Network: inputstream is = conn. getinputstream ();
6) write out the obtained file stream: outstream. Write (buffer, 0, Len );
Summary:
-- When operating on large files, you need to write the files to sdcard instead of directly writing them to the phone memory.
-- To operate on a large file, you must read the file from the network and write it on the sdcard to reduce the memory usage of the mobile phone. This is very important and will be frequently asked during interviews.
-- Remember to close the file stream as soon as the operation is complete.

3. Send request parameters to the Internet
1) Save the address and parameters to the byte array: byte [] da Ta = Params. tostring (). getbytes ();
2) create a URL object: URL realurl = new URL (requesturl );
3) send a request to the network address through the httpurlconnection object: httpurlconnection conn = (httpurlconnection) realurl. openconnection ();
4) set the allowed output: conn. setdooutput (true );
5) set not to use cache: conn. setusecaches (false );
6) set the POST method for sending: conn. setrequestmethod ("Post ");
7) set to maintain persistent connections: conn. setrequestproperty ("connection", "keep-alive ");
8) set the file Character Set: conn. setrequestproperty ("charset", "UTF-8 ");
9) set the file length: conn. setrequestproperty ("Content-Length", String. valueof (DA Ta. Length ));
10) set the file type: conn. setrequestproperty ("Content-Type", "application/X-WWW-form-urlencoded ");
11) output as a stream.
Summary:
-- The POST request must be set to allow output.
-- Do not use cache, which is prone to problems.
-- When the setrequestproperty () Setting of the httpurlconnection object is started, the HTML file header is generated.

4. Send XML data to the Internet
The XML format is a standard communication language. The Android system can transmit data by sending XML files.
1) write the generated XML file into the byte array and set it to UTF-8: byte [] xmlbyte = xml. tostring (). getbytes ("UTF-8 ");
2) create a URL object and specify the address and parameter: URL url = new URL ("http: // localhost: 8080/itcast/contanctmanage. do? Method = readxml ");
3) obtain the link: httpurlconnection conn = (httpurlconnection) URL. openconnection ();
4) set connection Timeout: conn. setconnecttimeout (6*1000 );
5) set to allow the output of conn. setdooutput (true );
6) set not to use cache: conn. setusecaches (false );
7) set the post transmission mode: conn. setrequestmethod ("Post ");
8) maintain a persistent connection: conn. setrequestproperty ("connection", "keep-alive ");
9) set Character Set: conn. setrequestproperty ("charset", "UTF-8 ");
10) set the total length of the file: conn. setrequestproperty ("Content-Length", String. valueof (xmlbyte. Length ));
11) set file type: conn. setrequestproperty ("Content-Type", "text/XML; charset = UTF-8 ");
12) Send XML data as a file stream: outstream. Write (xmlbyte );
Summary:
-- We use HTML to transfer files. This method can only transmit files that are generally at 5 MB.
-- The transmission of large files is not suitable for the use of HTML. To transmit large files, we need to develop socket-oriented programming to ensure Program Stability.

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.