1. httpurlconnectionConnectionURL
1) create a URL object
URL url = new URL (http://www.baidu.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) // obtain the webpage from the Internet, send a request, and read the webpage back as a stream
Throw new runtimeexception ("request URL failed ");
5) Get the input stream returned by the Network
Inputstream is = conn. getinputstream ();
6) string result = readdata (is, "GBK"); // input the file stream with outstream. Write
7) Conn. Disconnect ();
Summary:
-- 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.
-- The operations on file streams in Android are the same as those in Java SE.
-- 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.
2.DirectionInternetSend request parameters
Steps:
1) create a URL object: URL realurl = new URL (requesturl );
2) send a request to the network address through the httpurlconnection object
Httpurlconnection conn = (httpurlconnection) realurl. openconnection ();
3) set the allowed output: conn. setdooutput (true );
4) set not to use cache: conn. setusecaches (false );
5) set the POST method for sending: conn. setrequestmethod ("Post ");
6) set to maintain a persistent connection: conn. setrequestproperty ("connection", "keep-alive ");
7) set the file Character Set: conn. setrequestproperty ("charset", "UTF-8 ");
8) set the file length: conn. setrequestproperty ("Content-Length", String. valueof (data. Length ));
9) set the file type: conn. setrequestproperty ("Content-Type", "application/X-WWW-form-urlencoded ");
10) 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.
3. Direction Internet Send XML Data
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
-- Save the address and parameters to the byte array: byte [] DATA = Params. tostring (). getbytes ();