Internet access URL for Android

Source: Internet
Author: User

Thread visitbaiduthread = new Thread (new visitwebrunnable ());
Visitbaiduthread.start ();
try {
Visitbaiduthread.join ();

System.out.println ("xiancheng======");

} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

----------------------

Class Visitwebrunnable implements runnable{

@Override
public void Run () {
TODO auto-generated Method Stub
String data = Geturlresponse ("http://www.bjtime.cn");
String resultstr = data;
System.out.println ("ddddd----" +resultstr);
}
private string Geturlresponse (String urlstring) {
HttpURLConnection conn = null; Connection object
InputStream is = null;
Resultdata = "";
try {
URL url = new URL (urlstring); URL Object
conn = (httpurlconnection) url.openconnection (); Open a link using a URL
Conn.setdoinput (TRUE); Allow input stream, that is, allow download
Conn.setdooutput (TRUE); Allow output stream, i.e. allow upload
Conn.setusecaches (FALSE); Do not use buffering
Conn.setrequestmethod ("GET"); Using GET Requests
is = Conn.getinputstream (); Gets the input stream, at which point the link is actually established
InputStreamReader ISR = new InputStreamReader (IS);
BufferedReader bufferreader = new BufferedReader (ISR);
String inputline = "";
while ((Inputline = Bufferreader.readline ()) = null) {
Resultdata + = Inputline + "\ n";
}

} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
if (is = null) {
try {
Is.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
IF (conn! = null) {
Conn.disconnect ();
}
}

return resultdata;
}

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.