Android Network communications (1)

Source: Internet
Author: User

Use Httpurlconnecation for communication

1. GET Request:

New Thread () {public void run () {try{//1. Connect network  Create URL object url url = new URL ("http://www.baidu.com");//2. Open Connection  And get HttpURLConnection object httpurlconnection Huc = (httpurlconnection) url.openconnection ();//3. Reads the data huc.setdoinput (true) of the object resource,//sets the Read data Huc.setrequestmethod ("GET");//sets the request Mode  gethuc.connect ();//Establish link success// Get read stream InputStream is = Huc.getinputstream (); BufferedReader br = new BufferedReader (new InputStreamReader (IS))//4. Gets the character data read to  create StringBuilder object StringBuilder SB = new StringBuilder ();//5. read data string line = "", while ((Line=br.readline ())!=null) {sb.append (line+ "\ n");//collect Read Data} Gets the Message object message msg = Message.obtain (); msg.obj = Sb;msg.what = 1;hd.sendmessage (msg);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}. Start ();

2. Post request:

New Thread () {public void run () {try{//1. Get URL object url url = new URL ("Http://wwwtest.rupeng.cn:8888/QueryAvatarInfo.ashx") ;//2. Get HttpURLConnection object HttpURLConnection Huc = (httpurlconnection) url.openconnection ();//3. setting Read in and write out Huc.setdoinput (true); Huc.setdooutput (True);//4. Set the request mode Huc.setrequestmethod ("POST");//5. Get write stream OutputStream OS = Huc.getoutputstream (); O S.write (("Action=getallnames"). GetBytes ()); Os.flush ();//6. Link huc.connect ();//7. Read return data InputStream is = Huc.getinputstream (); BufferedReader br = new BufferedReader (new InputStreamReader (IS)); StringBuilder sb = new StringBuilder (); String line= ""; while ((Line=br.readline ())!=null) {sb.append (line+ "\ n");} Message msg = Message.obtain (); msg.obj = Sb;msg.what = 1;//send Data hd.sendmessage (msg);//Close Resource is.close (); Br.close (); O S.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}};}. Start ();

Use HttpClient for communication

GET

New Thread () {///time-consuming operation public void run () {///1, creating HttpClient object  HttpClient HC = new Defaulthttpclient ();//2. Setting the request mode HttpGet HG = new HttpGet ("http://www.baidu.com/"); Try{//3. The client performs a GET request to get the answer object HttpResponse hr = Hc.execute (Hg);//4. Get through the Answer object The Entity data Object Httpentity he = Hr.getentity ();//5. Parse the data by Entityutils tool class string, "entityutils.tostring" (He, "UTF-8");// Send data via handler message msg = Message.obtain (); msg.obj = Data;msg.what = 1;hd.sendmessage (msg);} catch (Clientprotocolexception e) {//Todo auto-generated catch Blocke.printstacktrace ()} catch (Exception e) {//Todo Aut O-generated catch Blocke.printstacktrace ();}}. Start ();

  

Android Network communications (1)

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.