"Turn" Pro Android Learning Note (69): HTTP Service (3): HTTP POST MultiPart

Source: Internet
Author: User
Tags http post

Directory (?) [-]

    1. Setting up a test environment
    2. Development environment Import third-party jar
    3. HTTP Post Multipart Small example

HTTP post not only passes parameters through key-value pairs, but also carries more complex parameters, such as files. HTTP Post Support carries Body,content-type to multipart. This time we will try to upload a small file via HTTP post.

Setting up a test environment

Upload the file test environment is very difficult to find, download tomcat, want to install an environment, open, found to forget the same, to see the help, more trouble, the following is I upload files to 189 mesh capture package, upload a picture file, the entire content length of 39K. The reason the network disk is difficult to test environment is that we must complete a series of operations such as login. We just need to verify the small example, can simply be determined by grasping the package, the Web server is expected to return 4xx or 5xx errors, but this does not affect our experiment.

Development environment: Importing third-party jars

But currently Andriod's httpclient does not directly support multipart, we need three additional jars to support: Apache Commons io,mime4j, and Httpmime. It can be downloaded from the address below, and will be attached to the source code of the small example we provide. http://commons.apache.org/proper/commons-io/,http://james.apache.org/mime4j/, and http://hc.apache.org/index.html. These jars are ultimately packaged in the APK to be used by the app to run on the device.

In previous ADT versions, you can right-click on the appropriate project, see Android Learning Note (44): Use of third-party Java libraries. Select Properties, go in and choose Java Build Path, then select Libaraies and click Add External jars. However, in the latest version, this way in the compilation of normal, at run time will be reported Noclassdeffounderror, as follows.

The reason is that these jar packages do not really break into the apk. In the new ADT, we will directly copy the corresponding jar package to the libs/directory under Project, then click Libs/in Eclipse, right button and select Refresh. We can see the corresponding jar in both libs/and Android Private libraries/, as shown in:

If we import the latest httpmime-4.3.1.jar, we need to import the Httpcore-4.3.jar at the same time, in the next code to write to the Inputstreambody data type, Inputstreambody will use the Httpcore class, whether the runtime will Reported Org.apache.http.entity.ContentType cannot be resolved error.

HTTP Post Multipart Small example

HTTP POST multipart is also an example of HTTP post, which, compared to the previous small example, just sets the HTTP parameter, this example sets the multipart parameter. The following section of the example, how to read response the same as before, no longer repeat.

Private HttpResponse Multiparttest () throws exception{
try{
HttpClient client = new Defaulthttpclient ();
HttpPost request = new HttpPost ("http://upload.cloud.189.cn/v5/v5webUploadSmallFileActionl");

//Set HTTP parameters, this example sets the multipart parameter
(1) Upload file readme.txt has been placed under asset/, get the contentbody of the file. If it is in a directory in the storage, you can use filebody fb = new Filebody (File f) to get the contentbody.
InputStream is = This.getassets (). Open ("Readme.txt");
byte[] data = Ioutils.tobytearray (IS);
Inputstreambody ISB = new Inputstreambody (new Bytearrayinputstream (data), "Myupload");
(2) In addition to the file, we add two parameters to generate the contentbody of the two parameters
Stringbody sb1 = new Stringbody ("Some text goes Here");
Stringbody SB2 = new Stringbody ("Some text goes here, too");
(3) as multipart parameter, add to request
multipartentity multientity = new multipartentity ();
Multientity.addpart ("Myupload", ISB);
Multientity.addpart ("One", SB1);
Multientity.addpart ("Both", SB2);
Request.setentity (multientity);
In the last example, the parameter in Setentity () is urlencodedformentity this time is multipartentity

return Client.execute (Request);
}catch (Exception e) {
Throw e;
}
}

Back to talk about the commissioning environment, we find an HTTP server on the Internet, this example is a 189 network disk, send it an HTTP POST multipart request, because we do not have a network disk user login process, and the parameters are not 189 specified, so reponse received 404 messages. But we can find out how to fill the multipart in Android by grasping the package tool to see how the parameters are filled in. In this example, we have added three parameters to the multipart entity, so let's look at the request package.

If we are on the machine state Tomcat Apache to build the test environment, it should be noted that in the simulator can not use 127.0.0.1来 the Tomcat server pointing to the machine, which points to the emulator itself, using the address of the NIC.

The example code covered in this blog post can be downloaded in the Pro Android Learning: Http Service Small example.

RELATED Links: My Android development related articles

"Turn" Pro Android Learning Note (69): HTTP Service (3): HTTP POST MultiPart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.