[Android] upload files to the server
Asynchronous HTTP framework post submits data to the server
Previously, we used the common method of post submission of data, which is more troublesome. Now we use the asynchronous framework to implement the following, which is very comfortable.
Get asynchttpclient object
Call the post (URI, params, responsehandler) method of asynchttpclient object to submit data. Parameters:
Uri is a path of type string
Params is a requestparams object, similar to a map set, which puts in key value pairs
Responsehandler is the responsehandler object, interface type,
Directly implement the new class asynchttppresponsehandler and override the onsuccess () method and onerror () method
Upload file to server
First, you need to build a file upload server,
Open J2EE for eclipse to create a new web project,
It's troublesome to do it yourself. You need to use some file upload frameworks, common file upload and common io. Create a new servlet to handle the upload, and copy the two jar packages to the webcontent / WEB-INF / lib directory
Use the JSP file to write a form, test the file upload, and check the HTTP protocol. You can see that the file upload is actually a post request with different header information
Content-Type:multipart/form-data; Boundary = ------------------ 126555457. This boundary is a randomly generated dividing line
Content-Disposition:form-data; name=”xxx”; filename=”xxx”; Content-Type:xxxx
Input stream
If there is -- after the split line represents the end of the data, the original upload needs to be oriented to the HTTP protocol, which is very troublesome to write and needs to splice various data. Now it is very simple to use the framework
Page layout, an editext fills in the file path, a button click to upload, and the linear layout is arranged vertically
Get asynchttpclient object
Call the post (URI, responsehandler) method of asynchttpclient object, post submit data, parameters:
Uri is a path of type string
Params is the requestparams object, similar to the map set. Put in the key value pair and call the put () method of the requestparams object. The key value pair is the file object, and the new out file object wraps the file path
Responsehandler is the responsehandler object, interface type,
Directly implement the new class asynchttppresponsehandler and override the onsuccess () method and onerror () method