Android's HttpURLConnection
1.HttpURLConnection Connection URL
1) Create a URL object
URL url = new URL (http://www.baidu.com);
2) Get web page data from the network using HttpURLConnection objects
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
3) Set the connection timeout
Conn.setconnecttimeout (6*1000);
4) to determine the response code
if (Conn.getresponsecode ()! = 200)//Get the Web page from the Internet, send a request, and read the page back in the form of 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"); File stream input out file with Outstream.write
7) Conn.disconnect ();
Summary :
--Remember to set the connection timeout and if the network is not good, the Android system will retract the resource interruption operation over the default time.
--Returns the response code 200, which is successful.
--The operation of the file stream in Android is the same as in Java SE.
--When working with large files, write the files to SDcard and do not write directly to the phone memory.
--operation of large files is to read from the network again, to write to SDcard, reduce the use of cell phone memory. This is important, and interviews are often asked.
--When you are done with the file flow, remember to close it promptly.
2. Send request parameters to the Internet
Steps:
1) Create URL object: url realurl = new URL (requesturl);
2) Send the request to the network address via the HttpURLConnection object
HttpURLConnection conn = (httpurlconnection) realurl.openconnection ();
3) Set allowable output: Conn.setdooutput (True);
4) Set not to use cache: Conn.setusecaches (false);
5) set to send using post: Conn.setrequestmethod ("post");
6) set to maintain long connection: Conn.setrequestproperty ("Connection", "keep-alive");
7) Set file character set: Conn.setrequestproperty ("Charset", "UTF-8");
8) Set File length: Conn.setrequestproperty ("Content-length", String.valueof (Data.length));
9) Set File type: Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
10) output in stream mode.
Summary :
--Send POST request must set allow output
--Do not use the cache, prone to problems.
--At the beginning of the Setrequestproperty () setting of the HttpURLConnection object, the HTML file header is generated.
3.toInternetSendXMLData
The XML format is the standard language for communication, and the Android system can also transmit data by sending an XML file.
1) write the resulting XML file into a byte array and set it to utf-8:byte[] Xmlbyte = xml.tostring (). GetBytes ("UTF-8");
2) Create the URL object and specify the address and parameters: url url = new URL (http://localhost:8080/itcast/contanctmanage.do?method=readxml);
3) Get link: httpurlconnection conn = (httpurlconnection) url.openconnection ();
4) Set Connection timeout: Conn.setconnecttimeout (6* 1000);
5) Set Allow output conn.setdooutput (true);
6) Set not to use cache: Conn.setusecaches (false);
7) settings are transmitted by post: Conn.setrequestmethod ("post");
8) Maintain long connection: Conn.setrequestproperty ("Connection", "keep-alive");
9) Set CharSet: Conn.setrequestproperty ("Charset", "UTF-8");
10) Total length of the Set 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);
Summarize:
--We use HTML to transfer files, which can only transfer files that are normally 5M in the same way.
--Transfer large files is not suitable for HTML, transfer large files We are going to program for sockets. Ensure the stability of the programs
--Store the address and parameters in a byte array: byte[] data = params.tostring (). GetBytes ();
Turn from
Android HttpURLConnection (Turn)