[Author] Zeng jiansheng
[Author mailbox] zengjiansheng1@126.com
[Author's QQ] 190678908
Author MSN zengjiansheng1@hotmail.com
[Author's blog] blog.csdn.net/newjueqi
**************************************** ***************************************
In Android client programming (especially SNS clients), you often need to implement the registration function Activity. You need to enter the user name, password, email address, and photo before registering. However, there is a problem in this case. You can use form in HTML to implement the above registry form. The required information will be automatically encapsulated as a complete HTTP protocol, but in Android, how do I encapsulate these parameters and files to be uploaded as HTTP?
Let's first try to see what information the form encapsulates.
Step 1: Compile a Servlet and save the received HTTP information in a file. The Code is as follows:
Public VoidDoPost (HttpServletRequest request, HttpServletResponse response)
ThrowsServletException, IOException {
// Obtain the input stream, which is the entity content in the HTTP protocol.
ServletInputStream sis = request. getInputStream ();
// Buffer zone
ByteBuffer [] =New Byte[1024];
FileOutputStream fos =NewFileOutputStream ("d: // file. log ");
IntLen = sis. read (buffer, 0, 1024 );
// Cyclically read the information in the stream into the file. log file.
While(Len! =-1)
{
Fos. write (buffer, 0, len );
Len = sis. readLine (buffer, 0, 1024 );
}
Fos. close ();
Sis. close ();
}
Step 2: Implement a form page such as 1, generate a registry ticket, and submit it to the Servlet
Figure 1
The detailed code is as follows:
<Form action = "servlet/ReceiveFile" method = "post" enctype = "multipart/form-data">
First parameter <input type = "text" name = "name1"/> <br/>
The second parameter <input type = "text" name = "name2"/> <br/>
The first uploaded file <input type = "file" name = "file1"/> <br/>
The second uploaded file <input type = "file" name = "file2"/> <br/>
<Input type = "submit" value = "submit">
</Form>
Note: To upload an attachment, you must set enctype to multipart/form-data to upload the attachment.
Step 3: After entering the information, click "Submit" and open the file. log file in drive d with notepad. The data is as follows:
----------------------------- 7d92221b604bc
Content-Disposition: form-data; name = "name1"
Hello
----------------------------- 7d92221b604bc
Content-Disposition: form-data; name = "name2"
World
----------------------------- 7d92221b604bc
Content-Disposition: form-data; name = "file1"; filename = "C:/2.GIF"
Content-Type: image/gif
GIF89a
Why?