Android Development HttpGet HttpPost HttpURLConnection httpClient

Source: Internet
Author: User

<p style= "margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:18px; Color:rgb (68, 68, 68); Font-family:tahoma, Arial, Sans-serif; ><strong><span style= "FONT-SIZE:18PX;" > One, httpclinet way </span></strong></p><p style= "margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:18px; Color:rgb (68, 68, 68); Font-family:tahoma, Arial, Sans-serif; ><strong><span style= "FONT-SIZE:18PX;" >1, HTTP GET example:</span></strong></p>
<span style= "FONT-SIZE:14PX;"   > 1 public class testhttpgetmethod{2 public void Get () {3 BufferedReader in = null;   4 try{5 HttpClient client = new Defaulthttpclient ();   6 HttpGet request = new HttpGet ();   7 Request.seturi ("http://w26.javaeye.com");    8 HttpResponse response = Client.execute (request);     9 in = new BufferedReader (New InputStreamReader (Response.getentity (). GetContent ()));   Ten StringBuffer sb = new StringBuffer ("");  String line = "";  A String NL = System.getproperty ("Line.separator");             (line = In.readline ())! = null) {Sb.append (line + NL); 15 16} 17  In.close ();  String page = sb.tostring ();  LOG.I (TAG, page); }catch (Exception e) {log.e (tag,e.tostring ())}finally{+ if (in! = nul  L) {try{In.close ();  }catch (IOException IoE) {log.e (TAG, ioe.tostring ()); (+}) (+)-}</span>

2. HTTP POST Example:

 <span style= "FONT-SIZE:14PX;"   >1 public class testhttppostmethod{2 public void post () {3 BufferedReader in = null;   4 try{5 HttpClient client = new Defaulthttpclient ();    6 HttpPost request = new HttpPost ("http://localhost/upload.jsp");   7 list<namevaluepair> postparams = new arraylist<namevaluepair> ();   8 Postparams.add (New Basicnamevaluepair ("filename", "Sex.mov"));  9 urlencodeformentity formentity = new urlencodeformentity (postparams);  Ten request.setentity (formentity);  HttpResponse response = Client.execute (request);     in = new BufferedReader (New InputStreamReader (Response.getentity (). GetContent ()));   StringBuffer sb = new StringBuffer ("");  String line = "";  The String NL = System.getproperty ("Line.separator");                (line = In.readline ()) = null) {17 Sb.append (line + NL);  In.close ();  String result = Sb.tostring ();  LOG.I (TAG, result); }catch (Exception e) {log.e (tag,e.tostring ())}finally{if (In! = nul  L) {try{in.close ();  }catch (IOException IoE) {log.e (TAG, ioe.tostring ()); (+})-}</span>


second, the HttpURLConnection way

<span style= "FONT-SIZE:14PX;" > 1 URL url = null; 2 HttpURLConnection conn = null; 3 InputStream in = null; 4 OutputStream out = null; 5 byte[] data = "test string". GetBytes ();    6 try{7 URL =new url ("Www.xxx.com/servlet"), 8 conn = (httpurlconnection) url.openconnection (); 9 10//Set connection Properties 11 Conn.setdooutput (TRUE);//Use a URL connection to output conn.setdoinput (true);//Use a URL connection to enter the Conn.setusecaches (false);//Ignore Cache Conn.setconnecttimeout (30000);//Set the connection timeout length, in milliseconds Conn.setrequestmethod ("post");//Set request mode, POST or GET,   Note: If the request address is a servlet address, it must be set to post 16 17//Set the request header to Conn.setrequestproperty ("Accept", "*/*"); 19 Conn.setrequestproperty ("Connection", "keep-alive"), Conn.setrequestproperty ("Accept-charset", "utf-8"); data = null) {out = Conn.getoutputstream (); out.write (data),}25 int code = Conn.getresponsecode (); 2 6 if (code ==200) {in = Conn.getinputstream ();//may be blocked by long len = Conn.getcontentlength (); byte[] B s = new byte[(int) len];//Returns the result byte array of int all = 0;31 int DN = 0;32 while (DN = In.read (BS, all, 1)) > 0) {All + = Dn;34 if (all = Len) {break;36}37}38}39}</span>





Android Development HttpGet HttpPost HttpURLConnection httpClient

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.