HTTP requests in Android

Source: Internet
Author: User

HttpClient

 Public classMainactivityextendsActivity {Privatebutton button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.buttonid); Buttonlistener Listener=NewButtonlistener ();    Button.setonclicklistener (listener); }    classButtonlistenerImplementsonclicklistener{@Override Public voidOnClick (view view) {//principle: No access to the network in the main threadNetworkthread NT =NewNetworkthread ();        Nt.start (); }            }        classNetworkthreadextendsthread{@Override Public voidrun () {//Create HttpClientHttpClient HttpClient =Newdefaulthttpclient (); //creates an object that represents the request, and the parameter is the server address of the access//http://www.baidu.comHttpGet HttpGet =NewHttpGet ("http://www.marschen.com/data1.html"); //execute the request to get the corresponding object returned by the server            Try{HttpResponse resp=Httpclient.execute (HttpGet); //Check that the appropriate status is normal. Check that the value of the status code is equal to                intCode =resp.getstatusline (). Getstatuscode (); if(Code = = 200){                    //extracting data from a corresponding objecthttpentity entity =resp.getentity (); InputStream in=entity.getcontent (); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (in)); String Line=Reader.readline (); LOG.D ("HTTP", "data bits obtained from the server:" +Line ); }            } Catch(Exception e) {e.printstacktrace (); }        }    } }

HTTP requests in Android

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.