Android HttpClient and androidhttpclient
The Apache package is a good package for android Internet access, and is also the most common class for android access networks.
The following describes how to use HttpClient to implement get and post requests.
1. Get request
HttpGet get = new HttpGet("http://www.baidu.com");HttpClient hClient = new DefaultHttpClient();httpResponse = hClient.execute(get);
2. Post request
Map <String, String> map = new HashMap <String, String> (); map. put ("id", id); map. put ("name", name); map. put ("permission", String. valueOf (permission); List <NameValuePair> list = new ArrayList <NameValuePair> (); if (map! = Null &&! Map. isEmpty () {for (Map. entry <String, String> entry: map. entrySet () {// iterator // key-Value Pair NameValuePair nameValuePair = new BasicNameValuePair (entry. getKey (), entry. getValue (); list. add (nameValuePair) ;}} UrlEncodedFormEntity entity = new UrlEncodedFormEntity (list, encode); // use post to submit data HttpPost post = new HttpPost (path); post. setEntity (entity); // in the Request body // Default client HttpClient client = httpClient; HttpResponse httpResponse = client.exe cute (post );
3. code example:
First get request
Import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import org. apache. http. httpEntity; import org. apache. http. httpResponse; import org. apache. http. client. clientProtocolException; import org. apache. http. client. httpClient; import org. apache. http. client. methods. httpGet; import org. apache. http. impl. defaultHttpClientConnection; import Org. apache. http. impl. client. defaultHttpClient; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity {private Button requestButton; private HttpResponse httpResponse; private HttpEntity entity; @ Overri De protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); requestButton = (Button) findViewById (R. id. requestButton); requestButton. setOnClickListener (new OnClickListener () {public void onClick (View v) {new Thread (new Downtest ()). start () ;}});} class Downtest implements Runnable {public void run () {// generate a request object and request HttpGet Get = new HttpGet ("http://www.baidu.com"); // generate an Http client object HttpClient hClient = new DefaultHttpClient (); // use an Http client to send the request object InputStream inputStream = null; try {httpResponse = hClient.exe cute (get); // The response returned by httpResponse // The returned response data is placed in entity = httpResponse. getEntity (); inputStream = entity. getContent (); BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream); String result = ""; String line = ""; while (line = reader. readLine ())! = Null) {result = result + line;} System. out. println (result);} catch (ClientProtocolException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {try {inputStream. close ();} catch (Exception e) {e. printStackTrace ();}}}}}
Next is the post request.
Public class AccountHttpUtils {// private static String PATH = "http: // 192.168.253.1: 8088/CallName/servlet/AccountServler"; private static HttpClient httpClient; public AccountHttpUtils (HttpClient httpClient) {this. httpClient = httpClient;} public static String sendHttpClient (String path, Map <String, String> map, String encode) {List <NameValuePair> list = new ArrayList <NameValuePair> (); if (map! = Null &&! Map. isEmpty () {for (Map. entry <String, String> entry: map. entrySet () {// iterator // key-Value Pair NameValuePair nameValuePair = new BasicNameValuePair (entry. getKey (), entry. getValue (); list. add (nameValuePair);} try {// encapsulate the Request Parameters in the form. UrlEncodedFormEntity entity = new UrlEncodedFormEntity (list, encode ); // use the post method to submit data HttpPost post = new HttpPost (path); post. setEntity (entity); // in the Request body // Default client HttpClient client = HttpClient; HttpResponse httpResponse = client.exe cute (post); if (httpResponse. getStatusLine (). getStatusCode () = 200) {HttpEntity httpEntity = httpResponse. getEntity (); InputStream inputStream = httpEntity. getContent (); return changeInputeStream (inputStream, encode) ;}} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (ClientProtocolExcepti On e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} return "";} /*** convert an input stream to a String * @ param inputStream * @ param encode * @ return */private static String changeInputeStream (InputStream inputStream, String encode) {// usually called memory stream, written in the memory ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); byte [] Data = new byte [1024]; int len = 0; String result = ""; if (inputStream! = Null) {try {while (len = inputStream. read (data ))! =-1) {data. toString (); outputStream. write (data, 0, len);} // result is the result = new String (outputStream. toByteArray (), encode); outputStream. flush (); outputStream. close (); inputStream. close ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} return result;} public static String set (String id, String name, int permission) {// TODO Auto-generated method stub Map <String, string> map = new HashMap <String, String> (); map. put ("id", id); map. put ("name", name); map. put ("permission", String. valueOf (permission); String result = AccountHttpUtils. sendHttpClient (AbstractHttpUtils. PATH + "servlet/AccountServler", map, "UTF-8"); System. out. println ("result:" + result); return result ;}}
4. The get request accesses Baidu and returns the source code of the Baidu homepage.
Post is a class in one of my small projects
But the structure is clear ....
Keep the backup ......
Http://www.cnblogs.com/jycboy/p/httpclient.html