Android handy file + parameter upload request

Source: Internet
Author: User

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

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.