美文网首页
Android根据域名获取Ip

Android根据域名获取Ip

作者: 素颜的你 | 来源:发表于2019-03-16 11:42 被阅读0次

    根据域名获取ip

    package cn.ztuo.config;
    
    import android.os.AsyncTask;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.concurrent.ExecutionException;
    
    public class Domain2IP {
    
        public static String getINetAddress(String host) {
    
            MyAsyncTask myAsyncTask = new MyAsyncTask();
            AsyncTask<String, Integer, String> execute = myAsyncTask.execute(host);
            String s = null;
            try {
                s = execute.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            return s;
    
        }
    
        public static class MyAsyncTask extends AsyncTask<String, Integer, String> {
            public String ipAddress;
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected String doInBackground(String... strings) {
                String hostAddress = null;
                try {
                    InetAddress inetAddress = InetAddress.getByName(strings[0]);
                    hostAddress = inetAddress.getHostAddress();
    
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                return hostAddress;
            }
    
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                this.ipAddress=s;
    
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Android根据域名获取Ip

          本文链接:https://www.haomeiwen.com/subject/kzwjmqtx.html