Android notes: file + parameter upload requests

Source: Internet
Author: User

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. 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.


Note: How can I import the previously recorded data after the Android phone is flushed?

If you are using the Professional Edition, you can directly synchronize the newly installed notes;

If you are an ordinary version and you have backed up your computer, you can use the Backup recovery function;

My Android mobile phone keeps track of the changes in network data after the backup file is read?

What changes do you mean?
No,
Read the file you backed up. What is your backup like? What is it like after reading it?

Related Article

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.