Http API notes
The network connections API you have come into contact with are as follows:
1. HttpURLConnection
Four elements: 1, URL 2, HttpURLConnection 3, outputStream 4, inputStream
Uri.openConnection (); / / get HttpURLConnection.
Hc.setDoInput (true); / / allow input
Hc.setDoOutput (true); / / allow output
Hc.setRequestMethod ("POST"); / / set to post
Hc.setUseCaches (false); / / post does not use caching
Hc.setConnectTimeout (3000); / / set connection timeout
Hc.setReadTimeout (3000); / / set read timeout
Hc.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded")
Hc.setRequestProperty ("Charset", "UTF-8")
Os = hc.getOutputStream (); / / get the output stream
Os.write (vals)
Os.flush ()
If (hc.getResponseCode () = = HttpURLConnection.HTTP_OK)
Is = hc.getInputStream (); / / get the input stream
Get HttpURLConnection through URL.
OutputStream and inputStream are obtained by HttpURLConnection.
II. HttpClient
Four elements: 1, HttpParams 2, HttpClient 3, HttpPost/HttpGet 4, HttpEntity 5, HttpResponse
BasicHttpParams httpParams = new BasicHttpParams ()
HttpConnectionParams.setConnectionTimeout (httpParams
3000)
HttpConnectionParams.setSoTimeout (httpParams,3000)
HttpClient client = new DefaultHttpClient (httpParams)
HttpPost post = new HttpPost ("http://");
Post.setHeader ()
Post.setEntity ()
HttpResponse response = client.execute (post)
If (httpResponse.getStatusLine (). GetStatusCode () = 200)
Response.getEntity ()
Note: HttpEntity should use the appropriate class
Commonly used new ByteArrayEntity (content.getBytes ("UTF-8"))
UrlEncodedFormEntity / / list of key-value pairs
Note: Content-Type
Text: for standardized representation of text information, text messages can be in multiple character sets or formats
Multipart: multiple parts used to connect the body of the message form a message, which can be different types of data
Application: used to transfer application data or binary data
Message: used to wrap an E-mail message
Image: used to transfer static picture data
Audio: used to transmit audio or audio data
Video: used to transmit moving image data, which can be a video data format edited with audio.
HTML 5 enctype attribute
The enctype attribute specifies how the form data is encoded before it is sent to the server.
Application/x-www-form-urlencoded encodes all characters before sending (default).
Multipart/form-data does not encode characters. This value is required when using a form with file upload controls.
Text/plain converts spaces to "+" symbols, but does not encode special characters.