Sixth chapter, get network Request demo (Android)

Source: Internet
Author: User

Add Network permissions in Androidmanifest.xml

<uses-permission android:name= "Android.permission.INTERNET"/>
Mainactivity

Package Com.example.demo10;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.util.entityutils;import Android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.os.Message;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.linearlayout;import Android.widget.textview;public class Mainactivity extends Activity implements Onclicklistener {private TextView tv;private String str; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);//Comment out the layout of itself//Setcontentview (R.layout.activity_main);//Instantiate a layout linearlayout Llayout = new LinearLayout (this);//Set Layout direction llayout.setorientation (linearlayout.vertical);// Add the layout to the window This.setcontentview (llayout);//Generate button Button button = New button (this); button. SetText ("Send Get"); Button.setonclicklistener (this); Llayout.addview (button);//Generate TEXTVIEWTV = new TextView (this); Llayout.addview (TV);} @Overridepublic void OnClick (View arg0) {//TODO auto-generated method stub//open Child thread New thread () {public void run () {//Build G ET request HttpGet HG = new HttpGet ("http://www.sina.com"); HttpClient HC = new defaulthttpclient (); try {HttpResponse hr = Hc.execute (Hg); Httpentity he = hr.getentity ();//Convert to String//entityutils.tostring (He, "Utf-8"), after which a parameter can be filled with "utf-8", "gb2312", etc.,//can also not fill str = Entityutils.tostring (He);//Send Empty message handler.sendemptymessage (0);} catch (Exception e) {e.printstacktrace ();}};}. Start ();} Instantiate Handlerhandler handler = new Handler () {public void Handlemessage (Message msg) {tv.settext (str);};}
Run:


Sixth chapter, get network Request demo (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.