Android note-as-you-go file + parameter upload request
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. apache. http. entity. mime. MultipartEntity;
Import org. apache. http. entity. mime. content. FileBody;
Import org. apache. http. entity. mime. content. StringBody;
Import org. apache. http. impl. client. DefaultHttpClient;
Import org. apache. http. util. EntityUtils;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Public class testphone extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. face_recognition_activity );
New Thread (){
Public void run (){
Try {
Mai ();
} Catch (Exception e ){
E. printStackTrace ();
}
};
}. Start ();
}
Private void mai () throws Exception {
// Upload images to the server
HttpPost httpPost = new HttpPost (server address );
// Set Transfer Parameters
File file1 = new File ("/mnt/sdcard/yangtest.txt ");
MultipartEntity reqEntity = new MultipartEntity ();
ReqEntity. addPart ("vice_id ",
New StringBody ("1", Charset
. ForName (org. apache. http. protocol. HTTP. UTF_8 )));
ReqEntity. addPart ("m ",
New StringBody ("15261651603", Charset
. ForName (org. apache. http. protocol. HTTP. UTF_8 )));
ReqEntity. addPart ("f ",
New StringBody ("66666666666", Charset
. ForName (org. apache. http. protocol. HTTP. UTF_8 )));
// If (! File1.getAbsoluteFile (). equals (""))
//{
// FileBody fileBody = new FileBody (file1 );
// ReqEntity. addPart ("pic", fileBody );
//}
//
// StringBody type = new StringBody ("wish ");
// ReqEntity. addPart ("type", type );
// If (type. equals ("wish ")){
// StringBody temp = new StringBody ("1 ");
// ReqEntity. addPart ("temp", temp );
//}
// Else
//{
// StringBody temp = new StringBody ("");
// ReqEntity. addPart ("temp", temp );
//}
HttpPost. setEntity (reqEntity );
// Obtain the default HttpClient
HttpClient httpclient = new DefaultHttpClient ();
// Get HttpResponse
HttpResponse httpResponse = httpclient.exe cute (httpPost );
// HttpStatus. SC _ OK indicates that the connection is successful.
If (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK)
{
// Obtain the returned string
String strResult = EntityUtils. toString (httpResponse. getEntity ());
System. out. println ("yes! ");
Log. I ("test", "no" + strResult. toString ());
}
Else
{
Log. I ("test", "no ");
}
}
}
This class can be used to directly upload file + String information and modify it as needed.
Finally, do not forget to add network permissions to the list file.