Access to the network in Android is a httpclient way, a jar package provided by Apache. Wirelessly has inherited the jar package, so Android ADT does not need to import the jar specifically, it can be used directly.
The following is the Mainactivity.java code:
Package Com.wyl.httptest2;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;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.client.defaulthttpclient;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;public Class Mainactivity extends Actionbaractivity {Button btn; EditText et; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); btn = (Button) Findviewbyid (R.id.btnid); Myonclicklistener L = new Myonclicklistener (); et = (EditText) Findviewbyid (R.id.edittextid); Btn.setonclicklistener (L) ;//Binding Listener}class Myonclicklistener implements Onclicklistener{@overridepublic void OnClick (View v) {///principle: The main thread in the UI does not have direct access to the network System.out.println ("= = Thread Name:" +thread.currentthread (). GetName ()); Getinternetthread myThread = new Getinternetthread (); Mythread.start ();}} Class Getinternetthread extends thread{string line = "I am Empty", @Overridepublic Void Run () {System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName ());//Gets the uristring URI in the EditText control = Et.gettext (). toString (); SYSTEM.OUT.PRINTLN ("uri value:" +uri);//access network//Get client Object httpclient = new Defaulthttpclient ();//Get Request object and pass in Urihttpget get = new HttpGet (URI);//client Send Request object try {HttpResponse resp = Client.execute (get);//Get Status code, if it is 200 description successful int code = Resp.getstatusline (). Getstatuscode (); if (code = = 200) {//Get return value InputStream in = Resp.getentity (). getcontent (); BufferedReader reader = new BufferedReader (new InputStreamReader (in)), line = Reader.readline ();}} catch (Clientprotocolexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO Auto-generated catch Blocke.printstacktrace ();} et.seTtext (line),///Set the value of EditText, this place will cause the program to crash, guess is workthread can not directly modify the main thread is the data in the Android UI. }}}
The Android page is as follows:
Android Access Network