Android: view the local Internet IP address.

Source: Internet
Author: User
[Java]
View plaincopyprint?
  1. String getnetip (string ipaddr ){
  2. URL infourl = NULL;
  3. Inputstream instream = NULL;
  4. Try {
  5. Infourl = new URL (ipaddr );
  6. Urlconnection connection = infourl. openconnection ();
  7. Httpurlconnection httpconnection = (httpurlconnection) connection;
  8. Int responsecode = httpconnection. getresponsecode ();
  9. If (responsecode = httpurlconnection. http_ OK)
  10. {
  11. Instream = httpconnection. getinputstream ();
  12. Bufferedreader reader = new bufferedreader (New inputstreamreader (instream, "UTF-8 "));
  13. Stringbuilder strber = new stringbuilder ();
  14. String line = NULL;
  15. While (line = reader. Readline ())! = NULL)
  16. Strber. append (LINE + "\ n ");
  17. Instream. Close ();
  18. Return strber. tostring ();
  19. }
  20. } Catch (malformedurlexception e ){
  21. // Todo auto-generated Catch Block
  22. E. printstacktrace ();
  23. } Catch (ioexception e ){
  24. // Todo auto-generated Catch Block
  25. E. printstacktrace ();
  26. }
  27. Return "";
  28. }
   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.

Related Article

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.