获取IPconfig信息android

时间:2012-06-06 19:32:30

标签: java android networking

我开发了一个Android应用程序,其中一个活动显示了一个IPconfig列表。我使用以下代码获取信息:

public String   s_dns1 ;
    public String   s_dns2;     
    public String   s_gateway;  
    public String   s_ipAddress;    
    public String   s_leaseDuration;    
    public String   s_netmask;  
    public String   s_serverAddress;
    DhcpInfo d;
    WifiManager wifii;

 wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE);
        d=wifii.getDhcpInfo();
        s_dns1="DNS 1: "+String.valueOf(d.dns1);
        s_dns2="DNS 2: "+String.valueOf(d.dns2);    
        s_gateway="Default Gateway: "+String.valueOf(d.gateway);    
        s_ipAddress="IP Address: "+String.valueOf(d.ipAddress); 
        s_leaseDuration="Lease Time: "+String.valueOf(d.leaseDuration);     
        s_netmask="Subnet Mask: "+String.valueOf(d.netmask);    
        s_serverAddress="Server IP: "+String.valueOf(d.serverAddress);

        info= (TextView) findViewById(R.id.info1);
        info.setText("\n"+s_dns1+"\n"+s_dns2+"\n"+s_gateway+"\n"+s_ipAddress+"\n"+s_leaseDuration+"\n"+s_netmask+"\n"+s_serverAddress);

在模拟器中,我得到以下内容:

DNS1:0
DNS2:0
Default gateway:0
IP address:0
Lease time:0
Subnet mask:-1
Server IP:0

然而,当我在我的Galaxy选项卡GT-P1000上测试APK时,我得到以下内容:

DNS1:16820416
DNS2:0
Default gateway:16820416
IP address:33597632
Lease time:86400
Subnet mask:16777215
Server IP:16820416

我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:0)

所有这些dnsinfo成员都是IP地址,他们使用int来存储地址。你必须将它转换为字符串。 懒人的方式:

byte[] ba = BigInteger.valueOf(ip).toByteArray();
InetAddress address = InetAddress.getByAddress(ba);
String s = address.toString();