HttpURLConnection Encapsulating Asynchronous Network requests

Source: Internet
Author: User

    /*** Implementation of Asynctask, to fetch the data in the background away from * the UI thread. */    Private classDownloadtaskextendsAsynctask<string, Void, string>{@Overrideprotectedstring Doinbackground (String ... urls) {Try {                returnLoadfromnetwork (urls[0]); } Catch(IOException e) {returngetString (R.string.connection_error); }        }        /*** Uses The logging framework to display the output of the fetch * operation in the log fragment. */@Overrideprotected voidOnPostExecute (String result) {log.i (TAG, result); }    }    /**initiates the fetch operation.*/    PrivateString Loadfromnetwork (String urlstring)throwsIOException {InputStream stream=NULL; String Str=""; Try{Stream=DownloadURL (urlstring); STR= Readit (Stream, 500); } finally {           if(Stream! =NULL) {stream.close (); }        }        returnstr; }    /*** Given A string representation of a URL, sets up a connection and gets * an input stream. * @paramurlstring A string representation of a URL. * @returnAn InputStream retrieved from a successful httpurlconnection. * @throwsjava.io.IOException*/    PrivateInputStream DownloadURL (String urlstring)throwsIOException {//begin_include (Get_inputstream)URL url =NewURL (urlstring); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setreadtimeout (10000/*milliseconds*/); Conn.setconnecttimeout (15000/*milliseconds*/); Conn.setrequestmethod ("GET"); Conn.setdoinput (true); //Start the queryConn.connect (); InputStream Stream=Conn.getinputstream (); returnstream; //end_include (Get_inputstream)    }    /**Reads An InputStream and converts it to a String. * @paramstream InputStream containing HTML from targeted site. * @paramlen Length of String, this method returns. * @returnString concatenated according to Len parameter. * @throwsjava.io.IOException *@throwsjava.io.UnsupportedEncodingException*/    PrivateString Readit (InputStream stream,intLenthrowsIOException, unsupportedencodingexception {reader Reader=NULL; Reader=NewInputStreamReader (Stream, "UTF-8"); Char[] buffer =New Char[Len];        Reader.read (buffer); return NewString (buffer); }


Invocation mode

New Downloadtask (). Execute ("http://www.google.com"); 

HttpURLConnection Encapsulating Asynchronous Network requests

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.