Android获取公网IP

如果向获取Android使用的公网IP 可以通过pv.sohu来获取公网IP ,方法比较稳定;

public  StringgetPublicIp() {

URL infoUrl =null;

        InputStream inStream =null;

        String ipLine ="";

        HttpURLConnection httpConnection =null;

        try {

//            infoUrl = new URL("http://ip168.com/");

            infoUrl =new URL("http://pv.sohu.com/cityjson?ie=utf-8");

            URLConnection connection = infoUrl.openConnection();

            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");

                }

Pattern pattern = Pattern

.compile("((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))");

                Matcher matcher = pattern.matcher(strber.toString());

                if (matcher.find()) {

ipLine = matcher.group();

                }

}

}catch (MalformedURLException e) {

e.printStackTrace();

        }catch (IOException e) {

e.printStackTrace();

        }finally {

try {

inStream.close();

                httpConnection.disconnect();

            }catch (IOException e) {

e.printStackTrace();

            }catch (Exception ex) {

ex.printStackTrace();

            }

}

//        Log.e("getNetIp", ipLine);

//        System.out.println(ipLine);

        return ipLine;

    }

你可能感兴趣的:(Android获取公网IP)