First, you mustGet the corresponding permissions in the middle of manifest. xml
<Uses-Permission Android: Name = "android. Permission. Internet"/> <uses-Permission Android: Name = "android. Permission. access_network_state"/>
Public class httpexampleactivity extends activity {Private Static final string debug_tag = "httpexample"; private edittext urltext; private textview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); urltext = (edittext) findviewbyid (R. id. myurl); textview = (textview) findviewbyid (R. id. mytext);} // when user CLI CKS button, callasynctask. // before attempting to fetch the URL, makes sure that there is a network connection. public void myclickhandler (view) {// gets the URL from the UI's text field. string stringurl = urltext. gettext (). tostring (); connectivitymanager connmgr = (connectivitymanager) getsystemservice (context. connectivity_service); networkinfo = connmgr. getactivenetworkinfo (); If (networkinfo! = NULL & networkinfo. isconnected () {New downloadwebpagetext(cmd.exe cute (stringurl);} else {textview. settext ("No network connection available. ") ;}// uses asynctask to create a task away from the main UI thread. this task takes a // URL string and uses it to create an httpurlconnection. once the connection // has been established, the asynctask downloads the contents of the webpage as // an inputstream. finally, the inputstream is converted into a string, which is // displayed in the UI by the asynctask's onpostexecute method. private class downloadwebpagetext extends asynctask {@ override protected string doinbackground (string... URLs) {// Params comes from the execute () call: Params [0] is the URL. try {return DownLoadURL (URLs [0]);} catch (ioexception e) {return "unable to retrieve web page. URL may be invalid. ";}} // onpostexecute displays the results of the asynctask. @ override protected void onpostexecute (string result) {textview. settext (result );}}}
// Given a URL, establishes an httpurlconnection and retrieves // The web page content as a inputstream, which it returns as // a string. private string DownLoadURL (string myurl) throws ioexception {inputstream is = NULL; // only display the first 500 characters of the retrieved // web page content. int Len = 500; try {URL url = new URL (myurl); httpurlconnection conn = (httpurlconnection) URL. openco Nnection (); Conn. setreadtimeout (10000/* milliseconds */); Conn. setconnecttimeout (15000/* milliseconds */); Conn. setrequestmethod ("get"); Conn. setdoinput (true); // starts the query Conn. connect (); int response = Conn. getresponsecode (); log. D (debug_tag, "the response is:" + response); is = Conn. getinputstream (); // convert the inputstream into a string contentasstring = readit (is, Len); Re Turn contentasstring; // makes sure that the inputstream is closed after the app is // finished using it.} finally {If (is! = NULL) {is. Close ();}}}
// Reads an inputstream and converts it to a string. public String readit (inputstream stream, int Len) throws ioexception, unsupportedencodingexception {reader = NULL; reader = new inputstreamreader (stream, "UTF-8 "); char [] buffer = new char [Len]; reader. read (buffer); return new string (buffer );}
Of course, you can also choose to convert to image output.CodeAs follows:
Inputstream is = NULL;... Bitmap bitmap = bitmapfactory. decodestream (is); imageview = (imageview) findviewbyid (R. Id. image_view); imageview. setimagebitmap (Bitmap );