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