Android Post get example
Package alex. Reader. ebook. Bam;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. iterator;
Import java. util. List;
Import java. util. Map;
Import org. Apache. http. httpresponse;
Import org. Apache. http. namevaluepair;
Import org. Apache. http. Client. clientprotocolexception;
Import org. Apache. http. Client. httpclient;
Import org. Apache. http. Client. entity. urlencodedformentity;
Import org. Apache. http. Client. Methods. httpget;
Import org. Apache. http. Client. Methods. httppost;
Import org. Apache. http. Client. Params. httpclientparams;
Import org. Apache. http. impl. Client. defaulthttpclient;
Import org. Apache. http. Message. basicnamevaluepair;
Import org. Apache. http. Params. basichttpparams;
Import org. Apache. http. Params. httpconnectionparams;
Import org. Apache. http. Params. httpparams;
Import org. Apache. http. Params. httpprotocolparams;
Import org. Apache. http. Protocol. HTTP;
Import org. Apache. http. util. entityutils;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. widget. edittext;
Public class simpleclient extends activity {
Private httpparams;
Private httpclient;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. simple_client );
Edittext = (edittext) This. findviewbyid (R. Id. edittext01 );
List <namevaluepair> Params = new arraylist <namevaluepair> ();
Params. Add (New basicnamevaluepair ("email", "firewings.r@gmail.com "));
Params. Add (New basicnamevaluepair ("password", "954619 "));
Params. Add (New basicnamevaluepair ("remember", "1 "));
Params. Add (New basicnamevaluepair ("from", "kx "));
Params. Add (New basicnamevaluepair ("login", "login "));
Params. Add (New basicnamevaluepair ("refcode ",""));
Params. Add (New basicnamevaluepair ("refuid", "0 "));
Map params2 = new hashmap ();
Params2.put ("Hl", "ZH-CN ");
Params2.put ("Source", "HP ");
Params2.put ("Q", "Haha ");
Params2.put ("AQ", "F ");
Params2.put ("AQI", "G10 ");
Params2.put ("AQL ","");
Params2.put ("OQ ","");
String url2 = "http://www.google.cn/search ";
String url = "http://wap.kaixin001.com/home ";
Gethttpclient ();
Edittext. settext (dopost (URL, Params ));
// Edittext. settext (doget (url2, params2 ));
}
Public String doget (string URL, map Params ){
/* Create an httpget object */
String paramstr = "";
iterator iter = Params. entryset (). iterator ();
while (ITER. hasnext () {
map. entry entry = (map. entry) ITER. next ();
Object key = entry. getkey ();
Object val = entry. getvalue ();
paramstr + = paramstr = "&" + key + "=" + val;
}
If (! Paramstr. Equals ("")){
Paramstr = paramstr. replacefirst ("&","? ");
URL + = paramstr;
}
Httpget httprequest = new httpget (URL );
String strresult = "dogeterror ";
Try {
/* Send the request and wait for the response */
Httpresponse = httpclient.exe cute (httprequest );
/* If the status code is 200 OK */
If (httpresponse. getstatusline (). getstatuscode () = 200 ){
/* Read returned data */
Strresult = entityutils. tostring (httpresponse. getentity ());
} Else {
Strresult = "error response :"
+ Httpresponse. getstatusline (). tostring ();
}
} Catch (clientprotocolexception e ){
Strresult = E. getmessage (). tostring ();
E. printstacktrace ();
} Catch (ioexception e ){
Strresult = E. getmessage (). tostring ();
E. printstacktrace ();
} Catch (exception e ){
Strresult = E. getmessage (). tostring ();
E. printstacktrace ();
}
Log. V ("strresult", strresult );
Return strresult;
}
Public String dopost (string URL, list <namevaluepair> Params ){
/* Create an httppost object */
Httppost httprequest = new httppost (URL );
String strresult = "doposterror ";
try {
/* add request parameters to the request object */
httprequest. setentity (New urlencodedformentity (Params, HTTP. utf_8);
/* send the request and wait for the response */
httpresponse = httpclient.exe cute (httprequest );
/* Status Code 200 OK */
If (httpresponse. getstatusline (). getstatuscode () = 200) {
/* read returned data */
strresult = entityutils. tostring (httpresponse. getentity ();
} Else {
Strresult = "error response :"
+ Httpresponse. getstatusline (). tostring ();
}
} Catch (clientprotocolexception e ){
Strresult = E. getmessage (). tostring ();
E. printstacktrace ();
} Catch (ioexception e ){
Strresult = E. getmessage (). tostring ();
E. printstacktrace ();
} Catch (exception e ){
Strresult = E. getmessage (). tostring ();
E. printstacktrace ();
}
Log. V ("strresult", strresult );
Return strresult;
}
Public httpclient gethttpclient (){
// Create httpparams to set HTTP parameters (this part is not required)
This. httpparams = new basichttpparams ();
// Set connection timeout, socket timeout, and socket cache size
Httpconnectionparams. setconnectiontimeout (HTTP, 20*1000 );
Httpconnectionparams. setsotimeout (httpparams, 20*1000 );
Httpconnectionparams. setsocketbuffersize (httpparams, 8192 );
// Set redirection. The default value is true.
Httpclientparams. setredirecting (httpparams, true );
// Set the User Agent
String useragent = "Mozilla/5.0 (windows; U; Windows NT 5.1; ZH-CN; RV: 1.9.2) Gecko/20100115 Firefox/3.6 ";
Httpprotocolparams. setuseragent (httpparams, useragent );
// Create an httpclient instance
// Note: httpclient = new httpclient (); commons httpclient
// Usage in. In Android 1.5, we need to use the default Implementation of Apache defaulthttpclient.
Httpclient = new defaulthttpclient (httpparams );
Return httpclient;
}
}
From:
http://blog.csdn.net/firewings_r/article/details/5374851