Android Access Network

Source: Internet
Author: User

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

Related Article

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.