主机名Android的InetAddress Ip地址查找
我正在尝试使用InetAddress从Android应用程序的主机名中获取计算机或Raspberry Pi的IP地址,问题是当我键入www.google.com时,它可以工作,但当我尝试PC****时,它是我的PC主机名,从cmd命令hostname获取的,它不工作 请帮帮我主机名Android的InetAddress Ip地址查找,android,android-asynctask,dns,hostname,inetaddress,Android,Android Asynctask,Dns,Hostname,Inetaddress,我正在尝试使用InetAddress从Android应用程序的主机名中获取计算机或Raspberry Pi的IP地址,问题是当我键入www.google.com时,它可以工作,但当我尝试PC****时,它是我的PC主机名,从cmd命令hostname获取的,它不工作 请帮帮我 public void SearchMachines(View v) { String netAddress = null; TextView localTextView = (TextView)fi
public void SearchMachines(View v) {
String netAddress = null;
TextView localTextView = (TextView)findViewById(R.id.tvScaned);
try
{
netAddress = new NetTask().execute("www.google.com").get();
localTextView.setText("Address: " + netAddress);
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
public class NetTask extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params)
{
InetAddress addr = null;
try
{
addr = InetAddress.getByName(params[0]);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
return addr.getHostAddress();
}
}
![Here is Logcat][1]
返回地址getHostAddress;。即使在捕获之后,您仍在使用addr,但它为null,因此您有一个NullPointerException
将该语句放入try块中,并放入returne.getMessage;在catch块中。以什么方式不起作用?它会崩溃吗?它没有显示IP地址吗。。。不幸的是,App_mobile已经停止。我从我的手机上测试了它,但没有进行模拟。logcat会告诉你它为什么会崩溃。请张贴相关部分。