1. Error code: stringbody strbtype = new stringbody ("123", charset. forname ("UTF-8 "));
2. Problem description: there is no problem during compilation. An exception "noclassdeffounderror: org. Apache. http. entity. Mime. content. stringbody" is reported after running"
3. Cause: for example, the package in libs1 is okay during compilation, but the APK generate
Android notes: file + parameter upload requests
Step 1: we need the support of two jars, which will be provided later.
Step 2: Create a project
Below is the most important code
Package com. example. testpaizhao;Import java. io. File;Import java. io. UnsupportedEncodingException;Import java. nio. charset. Charset;Import org. apache. http. HttpResponse;Import org. apache. http. HttpStatus;Import org. apache. http. client. HttpClient;Import org. apache. http. client. methods. HttpPost;Import org. a
The solution to HttpClient uploading Chinese garbled characters is as follows,
Anyone who has used HttpClient knows that you can use the addTextBody method to add text information to be uploaded. However, if you want to upload Chinese text or files with Chinese names, garbled characters may occur, the solution is actually very simple:
Step 1: Set the encoding method of MultipartEntityBuilder to UTF-8.
Builder. setCharset (Charset. forName (HTTP. UTF_8); // you can specify the Request Encoding f
People who have used HttpClient know that you can add text information to upload by addtextbody method, but if you want to upload Chinese, or if you have a Chinese name file, the problem is garbled, the solution is simple:The first step: Set the multipartentitybuilder encoding method to UTF-8.Step Two: Create the ContentType object and specify the UTF-8 encoding.Step three: Use addpart+stringbody instead of addtextbody. Such as:Stringbody
People who have used HttpClient know that you can add text information to upload by Addtextbody method, but if you want to upload Chinese, or if you have a Chinese name file, the problem is garbled, the solution is simple:The first step: Set the Multipartentitybuilder encoding method to UTF-8.Builder.setcharset (Charset.forname (HTTP. Utf_8)); //Step Two: Create the ContentType object and specify the UTF-8 encoding.Step three: Use addpart+ stringbody
People who have used HttpClient know that you can add text information to upload by Addtextbody method, but if you want to upload Chinese, or if you have a Chinese name file, the problem is garbled, the solution is simple:The first step: Set the Multipartentitybuilder encoding method to UTF-8.Builder.setcharset (Charset.forname (HTTP. Utf_8)); //Step Two: Create the ContentType object and specify the UTF-8 encoding.Step three: Use addpart+ stringbody
with the basic usage of the two request methods before we go into encapsulation.First, based on the implementation of httpclient file upload let's take a look at the most basic upload code1. Uploading files on the Android side/** * @Title: Updatefile * @Description: File upload * @throws */private void Updatefile () {//Create a new HttpClient class HttpClient Httpclien t = new defaulthttpclient ();//Post file upload httppost post = new HttpPost ("http://192.168.1.107:10010/MINATest/servlet/ Upl
Http://www.tuicool.com/articles/Y7reYb
1. OverviewIn this tutorial we will describe how to use HttpClient 4 for a multi-file upload operation .We will use http://echo.200please.com as the test server because it is public-facing and accepts most types of content.If you want to learn more and learn about other great things you can do with HttpClient -then take a look at the top HttpClient tutorials.2. Using the Addpart methodLet's start by studying the Multipartentitybuilder object to
Need to download httpclient under the Httpcomponents project under the Apache Company----------Address is http://hc.apache.org/downloads.cgiThe main use of the Httpmime-4.1.2.jar bagAndroid Client:The following is the JSP form that requested the action (for testing)( Simulation constructs the above request form:Private String url= "Http://192.168.2.189:8080/MyOrderMeal/AddFoodStyle";HttpClient httpclient= new Defaulthttpclient ();HttpPost httppost= new HttpPost (URL);Multipartentity mulentity =
Download HTTPCLIENT under HttpComponents under apache
---------- Address is http://hc.apache.org/downloads.cgi
Mainly used in the httpmime-4.1.2.jar package
Android client:
The following is the jsp form requesting action (for test)
(
Simulate the construction of the above request form:
Private String url = "http: // 192.168.2.189: 8080/MyOrderMeal/AddFoodStyle ";
HttpClient httpclient = new DefaultHttpClient ();HttpPost httpPost = new HttpPost (url );MultipartEntity mulentity = new Multipar
filebody. The above part array corresponds to httpentity here. The httpclient here are under Org.apache.http.client.methods. 1 public void Upload (String localfile) {2 closeablehttpclient httpClient = null; 3 closeablehttpresponse Response = NULL; 4 try {5 httpClient = Httpclients.createdefault (); 6 7//pass a common parameter and file to the following place Address is a servlet 8 httppost httppost = new HttpPost (URL_STR); 9 10//convert file to stream object FileBody11 filebody bin
settings, you can not configure, with the default parameters, here set the connection and timeout length (milliseconds)Requestconfig config = Requestconfig.custom (). Setconnecttimeout (30000). SetSocketTimeout (30000). Build ();try {HttpPost HttpPost = new HttpPost ("Http://api2.juheapi.com/cardrecon/upload");Filebody encapsulates the parameters of the file typeFilebody bin = new Filebody (file);Stringbody encapsulates string-type parametersStringbo
connection via releaseconnection after you finish.
2.1.2 HttpPost Mode
This way, similar to the above, but become a filebody. The part array above corresponds to httpentity here. The httpclient here are org.apache.http.client.methods.
public void Upload (String localfile) {closeablehttpclient httpclient = null;
Closeablehttpresponse response = null;
try {httpclient = Httpclients.createdefault ();
To pass an ordinary parameter and file to the following addres
(). Setconnecttimeout (30000). SetSocketTimeout (30000). Build ();try {HttpPost HttpPost = new HttpPost ("http://v.juhe.cn/certificates/query.php");Filebody encapsulating the parameters of the file typeFilebody bin = new Filebody (file);Stringbody encapsulates a parameter of type stringStringbody keybody = new Stringbody (key, Contenttype.text_plain);Stringbody
Recent work needs to be crawled from the network to upload a lot of pictures on the server, file upload with the Apache httpclient 4.3, recorded for later lookup!
The code is as follows:
/** * Example How to use Multipart/form encoded POST request. */public class Clientmultipartformpost {public static void main (string[] args) throws Exception {if args.
Length!= 1) {System.out.println ("File Path not given");
System.exit (1);
} closeablehttpclient httpclient = Httpcl
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.