The first step: two jar support is required and will be shown later.
Step Two: Build a project
The most important code is posted below
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{
Uploading images to the server
HttpPost HttpPost = new HttpPost (server address);
Setting Pass 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);
Get the default httpclient
HttpClient HttpClient = new Defaulthttpclient ();
Get HttpResponse
HttpResponse HttpResponse = Httpclient.execute (HttpPost);
HTTPSTATUS.SC_OK indicates successful connection
if (Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK)
{
Gets 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 directly upload the file +string information according to their own needs to change it
Finally, don't forget to add network permissions to the manifest file.
Android handy file + parameter upload request