Android uses the http POST transmission method and androidpost

Source: Internet
Author: User

Android uses the http POST transmission method and androidpost

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
// 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 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 that the client needs to send 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 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 ();
}
}


Which of the following is better for get and post in http transmission?

The get method is to encode the control name and value information of the Form Control and send it through the URL (you can see it in the address bar ). Post sends the form content over http. A get transmits a variable through a URL, and the total amount of data that can be transferred is smaller than the data that can be transferred using the post method. The get method attaches the data to be transmitted to the URL and delivers the data to the server together. Therefore, the amount of data transmitted is limited, but the execution efficiency is better than that of the post method. In fact, the post method can transmit data to the server without time restrictions, and the user cannot see this process on the browser side. Therefore, the post method is suitable for sending a confidential (such as a credit card number) or a large amount of data is sent to the server.

Get is more efficient and post is more secure. No one is willing to read post Data and the bank will never use get to submit the data;

Therefore, there is no better, only the specific situation which is more suitable.

Android 22 client-side code or tutorial (including the server receiving part) on how to upload complex structured data in http post Mode)

Assembled into xml and uploaded to the background for parsing

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.