Android post transmission over HTTP)

Source: Internet
Author: User

Http://chunpeng.iteye.com/blog/631972

Android post transmission over HTTP is as follows:

Method 1: httppost (import org. Apache. http. Client. Methods. httppost)

The Code is as follows:

Private button button1, button2, button3;
Private textview textview1;

Button1.setonclicklistener (New button. onclicklistener (){

@ Override
Public void onclick (view arg0 ){
// Todo auto-generated
Method stub
// URL Scheme
// String uriapi = "http://www.dubblogs.cc: 8751/Android/test/API/post/index. php ";

String uriapi = "http: // 172.20.0.206: 8082 // testservelt/login. Do ";

/* Create an http post connection */
Httppost httprequest = new httppost (uriapi );

// Post operation transfer variables must be stored in the namevaluepair [] Array
// PASS Parameters
The method obtained by the server is request. getparameter ("name ")
List <namevaluepair>
Params = new arraylist <namevaluepair> ();
Params. Add (New
Basicnamevaluepair ("name", "this is Post "));
Try {



// Send an HTTP request
Httprequest. setentity (New
Urlencodedformentity (Params, HTTP. utf_8 ));
// Get http Response

Httpresponse = new defaulthttpclient(cmd.exe cute (httprequest );


// If the status code is 200 OK

If (httpresponse. getstatusline (). getstatuscode () = 200 ){

// Retrieve the response string
String
Strresult = entityutils. tostring (httpresponse. getentity ());

Textview1.settext (strresult );
} Else {
Textview1.settext ("error
Response "+ httpresponse. getstatusline (). tostring ());
}

} Catch (clientprotocolexception e ){

Textview1.settext (E. getmessage (). tostring ());

E. printstacktrace ();
} Catch (unsupportedencodingexception e ){

Textview1.settext (E. getmessage (). tostring ());

E. printstacktrace ();
} Catch (ioexception e ){

Textview1.settext (E. getmessage (). tostring ());

E. printstacktrace ();
}
}

});

Method 2: httpurlconnection and URL (import java.net. httpurlconnection; import
Java.net. url; import java.net. urlencoder ;)

Private void httpurlconnection (){
Try {
String pathurl = "http: // 172.20.0.206: 8082/testservelt/login. Do ";

// Establish a connection
URL url = new URL (pathurl );
Httpurlconnection
Httpconn = (httpurlconnection) URL. openconnection ();


//// Set Connection Properties
Httpconn. setdooutput (true); // use a URL to connect to the output

Httpconn. setdoinput (true); // use URL Connection for Input

Httpconn. setusecaches (false); // ignore Cache

Httpconn. setrequestmethod ("Post"); // sets the URL request Method
String requeststring =
"The data sent from the client to the server as a stream ...";

// Set Request attributes

// Obtain the Data byte. the encoding of the request data stream must be consistent with the encoding of the Request stream processed by the following server.
Byte [] requeststringbytes =
Requeststring. getbytes (encoding_utf_8 );

Httpconn. setrequestproperty ("Content-Length", "" +
Requeststringbytes. Length );

Httpconn. setrequestproperty ("Content-Type ",
"Application/octet-stream ");

Httpconn. setrequestproperty ("connection", "keep-alive"); // maintain a persistent connection

Httpconn. setrequestproperty ("charset", "UTF-8 ");
//

String name = urlencoder. encode ("Huang Wuyi", "UTF-8 ");

Httpconn. setrequestproperty ("name", name );


// Create an output stream and write data
Outputstream =
Httpconn. getoutputstream ();

Outputstream. Write (requeststringbytes );

Outputstream. Close ();
// Obtain the response status
Int responsecode =
Httpconn. getresponsecode ();
If (httpurlconnection. http_ OK =
Responsecode) {// connection successful

// Process data when the response is correct

Stringbuffer sb = new stringbuffer ();
String
Readline;
Bufferedreader responsereader;

// Process the response stream, which must be consistent with the encoding of the server response stream output
Responsereader = new
Bufferedreader (New inputstreamreader (httpconn. getinputstream (),
Encoding_utf_8 ));
While (Readline = responsereader. Readline ())
! = NULL ){
SB. append (Readline). append ("\ n ");

}
Responsereader. Close ();

TV. settext (sb. tostring ());
}
} Catch (exception ex ){

Ex. printstacktrace ();
}
}

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.