1. httpclient the description of httpclient is used to simulate HTTP requests. In fact, the essence is to simulate HTTP requests and send them to the Web server;
Android has integrated httpclient, so it can be used directly;
Note: The httpclient Code is applicable not only to Android, but also to General Java programs;
Core http get code:
(1) defaulthttpclient client = new defaulthttpclient (); (2) httpget get = new httpget (string URL); // The URL here is http ://.... /path? Arg1 = Value &.... argn = value (3) httpresponse response = client.exe cute (get); // simulate the request (4) int code = response. getstatusline (). getstatuscode (); // response code (5) inputstream in = response. getentity (). getcontent (); // The data returned by the server
Core http post code:
(1) defaulthttpclient client = new defaulthttpclient (); (2) basicnamevaluepair pair = new basicnamevaluepair (string name, string value); // create a request header field, for example, content-type, text/plain (3) urlencodedformentity entity = new urlencodedformentity (list <namevaluepair> list, string encoding); // perform URL encoding on the custom request header (4) httppost post = new httppost (string URL); // The URL here is http ://.... /path (5) post. setentity (entity); (6) httpresponse response = client.exe cute (post); (7) int code = response. getstatusline (). getstatuscode (); (8) inputstream in = response. getentity (). getcontent (); // The data returned by the server
Ii. server code
The server code and the code that sends the request through urlconnection remain unchanged:
Package Org. xiazdong. servlet; import Java. io. ioexception; import Java. io. outputstream; import javax. servlet. servletexception; import javax. servlet. annotation. webservlet; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; @ webservlet ("/servlet1") public class servlet1 extends httpservlet {protected void doget (httpservletrequest Request, httpservletresponse response) throws servletexception, ioexception {string nameparameter = request. getparameter ("name"); string ageparameter = request. getparameter ("Age"); string name = new string (nameparameter. getbytes ("ISO-8859-1"), "UTF-8"); string age = new string (ageparameter. getbytes ("ISO-8859-1"), "UTF-8"); system. out. println ("get"); system. out. println ("name =" + name); system. out. println ("ag E = "+ age); response. setcharacterencoding (" UTF-8 "); outputstream out = response. getoutputstream (); // return data out. Write (" GET request succeeded! ". Getbytes ("UTF-8"); out. close ();} protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {request. setcharacterencoding ("UTF-8"); string name = request. getparameter ("name"); string age = request. getparameter ("Age"); system. out. println ("Post"); system. out. println ("name =" + name); system. out. println ("age =" + age); response. setcharacterencoding ("UTF-8 "); Outputstream out = response. getoutputstream (); Out. Write (" POST request successful! ". Getbytes (" UTF-8 "); Out. Close ();}}
3. Android client code
The effect is as follows:
Add the following in androidmanifest. xml:
<uses-permission android:name="android.permission.INTERNET"/>
Mainactivity. Java
Package Org. xiazdong. network. httpclient; import Java. io. bytearrayoutputstream; import Java. io. ioexception; import Java. io. inputstream; import java.net. urlencoder; 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. httpget; 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. toast; public class mainactivity extends activity {private edittext Name, age; private button getbutton, postbutton; private onclicklistener listener = new onclicklistener () {@ overridepublic void onclick (view v) {try {If (postbutton = V) {/** namevaluepair indicates a header. List <namevaluepair> stores all the header fields * urlencodedformentity is similar to urlencoder statements for URL encoding * httppost is similar to http post request * client.exe cute () similar to sending a request and returning response */defaulthttpclient client = new defaulthttpclient (); List <namevaluepa IR> List = new arraylist <namevaluepair> (); namevaluepair pair1 = new basicnamevaluepair ("name", name. gettext (). tostring (); namevaluepair pair2 = new basicnamevaluepair ("Age", age. gettext (). tostring (); list. add (pair1); list. add (pair2); urlencodedformentity entity = new urlencodedformentity (list, "UTF-8"); httppost post = new httppost ("http: // 192.168.0.103: 8080/Server/servlet1 "); post. setentity (entity); HT Tpresponse response = client.exe cute (post); If (response. getstatusline (). getstatuscode () = 200) {inputstream in = response. getentity (). getcontent (); // receives data from the server and displays string STR = readstring (in); toast on toast. maketext (mainactivity. this, STR, toast. length_short ). show ();} else toast. maketext (mainactivity. this, "post submission failed", toast. length_short ). show () ;}if (getbutton = V) {defaulthttpclient client = new defaulthttpcl Ient (); stringbuilder Buf = new stringbuilder ("http: // 192.168.0.103: 8080/Server/servlet1"); Buf. append ("? "); Buf. append ("name =" + urlencoder. encode (name. gettext (). tostring (), "UTF-8") + "&"); Buf. append ("age =" + urlencoder. encode (age. gettext (). tostring (), "UTF-8"); httpget get = new httpget (BUF. tostring (); httpresponse response = client.exe cute (get); If (response. getstatusline (). getstatuscode () = 200) {inputstream in = response. getentity (). getcontent (); string STR = readstring (in); toast. maketext (mainactivity. this, S Tr, toast. length_short ). show ();} else toast. maketext (mainactivity. this, "Get submission failed", toast. length_short ). show () ;}} catch (exception e) {}}; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); name = (edittext) This. findviewbyid (R. id. name); age = (edittext) This. findviewbyid (R. id. age); getbutton = (button) This. findviewbyid (R. id. get Button); postbutton = (button) This. findviewbyid (R. id. postbutton); getbutton. setonclicklistener (listener); postbutton. setonclicklistener (listener);} protected string readstring (inputstream in) throws exception {byte [] DATA = new byte [1024]; int length = 0; bytearrayoutputstream bout = new bytearrayoutputstream (); while (length = in. read (data ))! =-1) {bout. Write (data, 0, length);} return new string (bout. tobytearray (), "UTF-8 ");}}