Android implements Post to submit data to the server
Users familiar with web programming are familiar with get and post methods for passing form data.
Here do not specifically introduce the difference between get and post, For details, please refer to: http://www.bkjia.com/kf/201112/114558.html
The so-called get transmission data is also our most common one, such as http: // 127.0.0.1/index. php? Param = androidyue. This method is directly displayed in the url, so it is not safe,
Using post to transmit data is not directly exposed, which is more secure. The key and value are also required for post transmission.
The following is the sample code of the android program:
Package com. google. code. cakedroid. demo;
Import java. util. ArrayList;
Import java. util. List;
Import org. apache. http. HttpResponse;
Import org. apache. http. NameValuePair;
Import org. apache. http. client. entity. UrlEncodedFormEntity;
Import org. apache. http. client. methods. HttpEntityEnclosingRequestBase;
Import org. apache. http. client. methods. HttpPost;
Import org. apache. http. impl. client. DefaultHttpClient;
Import org. apache. http. message. BasicNameValuePair;
Import org. apache. http. protocol. HTTP;
Import org. apache. http. util. EntityUtils;
Import com. google. code. cakedroid. R;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. TextView;
Public class PostMethodDemoActivity extends Activity {
// Declare the variables
Private TextView tvResult;
Private Button btnClick;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
This. initViews ();
}
/*
* Initialize necessary views
*/
Private void initViews (){
This. initButtons ();
This. initTextViews ();
}
/*
* Initialize necessary textviews
*/
Private void initTextViews (){
This. tvResult = (TextView) this. findViewById (R. id. tvResult );
}
/*
* Initialize necessary buttons
*/
Private void initButtons (){
This. btnClick = (Button) this. findViewById (R. id. btnClick );
This. btnClick. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
PostData ();
}
});
}
/*
* Post data to remote host
*/
Private void postData (){
String destUrl = "http: // 10.0.2.2/form_handler.php ";
// Instantiate HttpPost object from the url address
HttpEntityEnclosingRequestBase httpRequest = new HttpPost (destUrl );
// The post name and value must be used as NameValuePair
List <NameValuePair> params = new ArrayList <NameValuePair> ();
Params. add (new BasicNameValuePair ("param", "I have posted you the data "));
Try {
HttpRequest. setEntity (new UrlEncodedFormEntity (params, HTTP. UTF_8 ));
// Execute the post and get the response from servers
HttpResponse httpResponse = new defaulthttpclient(cmd.exe cute (httpRequest );
If (httpResponse. getStatusLine (). getStatusCode () = 200 ){
// Get the result
String strResult = EntityUtils. toString (httpResponse. getEntity ());
TvResult. setText (strResult );
} Else {
TvResult. setText ("Error Response" + httpResponse. getStatusLine (). toString ());
}
} Catch (Exception e ){
System. out. println ("error occurs ");
}
}
}
Php code disconnection on the server:
<? Php
If (isset ($ _ POST ['param']) {
Echo $ _ POST ['param']. 'I have Ed the data ';
}
?>
NOTE: If normal access is required, add internet access permission to manifest. xml.
From BossDarcy's column