How to Implement the android http-post method example

Source: Internet
Author: User

Copy codeThe Code is as follows: package com. hl;

Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. Map;
Import java. util. Set;

Import org. apache. http. HttpEntity;
Import org. apache. http. HttpResponse;
Import org. apache. http. client. entity. UrlEncodedFormEntity;
Import org. apache. http. client. methods. HttpPost;
Import org. apache. http. impl. client. DefaultHttpClient;
Import org. apache. http. message. BasicNameValuePair;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;

Public class SimplePOST extends Activity {
Private TextView show;
Private EditText txt;
Private Button btn;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Show = (TextView) findViewById (R. id. show );
Txt = (edittext=findviewbyid(r.id.txt );
Btn = (Button) findViewById (R. id. btn );
Btn. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
Dopost (txt. getText (). toString ());

}
});
}

Private void dopost (String val ){
// Encapsulate data
Map <String, String> parmas = new HashMap <String, String> ();
Parmas. put ("name", val );

DefaultHttpClient client = new DefaultHttpClient (); // http client
HttpPost httpPost = new HttpPost ("http://mhycoe.com/test/post.php ");

ArrayList <BasicNameValuePair> pairs = new ArrayList <BasicNameValuePair> ();
If (parmas! = Null ){
Set <String> keys = parmas. keySet ();
For (Iterator <String> I = keys. iterator (); I. hasNext ();){
String key = (String) I. next ();
Pairs. add (new BasicNameValuePair (key, parmas. get (key )));
}
}

Try {
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity (pairs, "UTF-8 ");
/*
* Put POST data into an HTTP request
*/
HttpPost. setEntity (p_entity );
/*
* Send an actual http post request
*/
HttpResponse response = client.exe cute (httpPost );
HttpEntity entity = response. getEntity ();
InputStream content = entity. getContent ();
String returnConnection = convertStreamToString (content );
Show. setText (returnConnection );
} Catch (IllegalStateException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}

}

Private String convertStreamToString (InputStream is ){
BufferedReader reader = new BufferedReader (new InputStreamReader (is ));
StringBuilder sb = new StringBuilder ();
String line = null;
Try {
While (line = reader. readLine ())! = Null ){
Sb. append (line );
}
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
Is. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
Return sb. toString ();
}
}

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.