[Java]
View plaincopyprint?
- String getnetip (string ipaddr ){
- URL infourl = NULL;
- Inputstream instream = NULL;
- Try {
- Infourl = new URL (ipaddr );
- Urlconnection connection = infourl. openconnection ();
- Httpurlconnection httpconnection = (httpurlconnection) connection;
- Int responsecode = httpconnection. getresponsecode ();
- If (responsecode = httpurlconnection. http_ OK)
- {
- Instream = httpconnection. getinputstream ();
- Bufferedreader reader = new bufferedreader (New inputstreamreader (instream, "UTF-8 "));
- Stringbuilder strber = new stringbuilder ();
- String line = NULL;
- While (line = reader. Readline ())! = NULL)
- Strber. append (LINE + "\ n ");
- Instream. Close ();
- Return strber. tostring ();
- }
- } Catch (malformedurlexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- } Catch (ioexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- Return "";
- }
String GetNetIp(String ipaddr){ URL infoUrl = null; InputStream inStream = null; try { infoUrl = new URL(ipaddr); URLConnection connection = infoUrl.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connection; int responseCode = httpConnection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK) { inStream = httpConnection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); return strber.toString(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; }
View system. Out. println (getnetip ("http://fw.qq.com/ipaddress ")));
Add permission <uses-Permission Android: Name = "android. Permission. Internet"> </uses-Permission>
Obtain the Internet IP address from the http://fw.qq.com/ipaddresswebpage.