想对此代码发表您的意见。
private String getIpAddress(){
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while(enumNetworkInterfaces.hasMoreElements()){
NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
while(enumInetAddress.hasMoreElements()){
InetAddress inetAddress = enumInetAddress.nextElement();
String ipAddress = "";
if(inetAddress.isSiteLocalAddress()){
ipAddress = "SiteLocalAddress: ";
ip += inetAddress.getHostAddress() + "\n";
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
private void MDConnector() {
setMobileDataEnabled (MainActivity.this,true);
String ip = getIpAddress();
do {
addMessage("..")
try{
Thread.sleep(1000L);
}catch (InterruptedException localInterruptedException){
}
}while (ip.length() <= 0);
MDConnect();
}
它没有用。我的目标是:connect-&gt; getIPAddress-&gt;如果没有,请等到我得到一个&gt;然后转到另一个方法。 我的代码有什么问题?谁能帮我?提前谢谢。
编辑:添加了getIpAddress代码。积分到http://android-er.blogspot.jp/2014/02/get-my-ip-address.html
答案 0 :(得分:0)
在do-while循环中,ip-String不会被更改。尝试将获取ip-String IP的部分移动到try-block中。