getDhcpInfo获得android系统中错误的IP地址 [英] getDhcpInfo obtaining wrong ip Address in android

查看:530
本文介绍了getDhcpInfo获得android系统中错误的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一点点assistance.I'm试图获得一个WiFi网络的网关和子网掩码信息编程在Android中,然后用获得的串在我的application.I别处找到在这个网站的解决方案,这就是我已经实现了这样far.But的IP地址在设备上返回的是当我检查IPCONFIG我computer.So我的应用程序崩溃的不同。
当我中执行ipconfig我得到192.168.0.xx5,但我对TextView的活动显示一个不同的显示192.168.0.xx1..But因为我从我的XAMPP服务器下载数据,我需要它是相同的

下面是我的code:

 公共类DHCPInfo扩展活动实现OnClickListener {
公共字符串s_dns1;
公共字符串s_dns2;
公共字符串s_gateway;
公共字符串s_ipAddress;
公共字符串s_subnet;
公共字符串s_serverAddress;
TextView的dhcp_info,IP_ADDRESS;按钮btn_dhcp;
@燮pressWarnings(德precation)
DhcpInfo dhcpInfo;
WifiManager wifii;最后的处理程序H =新的处理程序();/ **当第一次创建活动调用。 * /
@燮pressWarnings(德precation)
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.dhcp);    wifii =(WifiManager)getSystemService(Context.WIFI_SERVICE);
    dhcpInfo = wifii.getDhcpInfo();    s_dns1 =DNS 1:+将String.valueOf(dhcpInfo.dns1);
    s_dns2 =DNS 2:+将String.valueOf(dhcpInfo.dns2);
    s_gateway =默认网关:+将String.valueOf(dhcpInfo.gateway);
    s_ipAddress =IP地址+将String.valueOf(dhcpInfo.ipAddress);
    s_subnet =子网掩码:+将String.valueOf(dhcpInfo.netmask);
    s_serverAddress =服务器IP:+将String.valueOf(dhcpInfo.serverAddress);
    INT ip地址= dhcpInfo.ipAddress;
    intToIpAddress(ip地址);    //显示它们
    dhcp_info =(的TextView)findViewById(R.id.text_dhcp);
    dhcp_info.setText(网络信息\\ n+ s_dns1 +\\ n+ s_dns2 +\\ n+ s_gateway +\\ n+ s_ipAddress +\\ n+ s_serverAddress);    IP_ADDRESS =(的TextView)findViewById(R.id.text_ip);
    dhcp_info.setText(IP地址\\ n+ intToIpAddress(ip地址));    btn_dhcp =(按钮)findViewById(R.id.btn_dhcp);
    btn_dhcp.setOnClickListener(本);
} 公共字符串intToIpAddress(INT ip地址){
    返回((ip地址和放大器; 0xFF的)+ +。
            ((ip地址>>> = 8)及为0xFF)+。 +
            ((ip地址>>> = 8)及为0xFF)+。 +
            ((ip地址>>> = 8)及为0xFF));
}
}


解决方案

据我了解,你有一台笔记本电脑和移动设备。两者都连接到同一WiFi网络与你的笔记本电脑工作作为Web服务器。这是正常的,他们有不同的IP地址 - 因为他们毕竟是不同的设备。 :)

如果你的路由器提供静态IP地址的设备(这是不可能的),你可以只硬code你的笔记本电脑在Java code的地址。这对调试生产或多或少确定的,但毫无用处 - 因为当你连接到你的present WiFi网络的设备应用程序只会工作

另外,你可以让你的Web应用程序的Internet访问(而不是只使用本地网络)。如果你的ISP是给你一个静态IP,您可以托管在笔记本电脑上应用。否则,你可以使用一些托管服务提供商 - 一些云服务应是一​​个确定的解决方案也和pretty便宜。

I need a little assistance.I'm trying to get the gateway and subnet mask details of a wifi network programmatically in android,then use the acquired string elsewhere in my application.I found a solution on this site and it's what i have implemented so far.But the ip Address returned on the device is different from when i check ipconfig on my computer.So my app crashes. when i execute ipconfig i get 192.168.0.xx5, but on my activity on the textview display a different one is shown 192.168.0.xx1..But since i am downloading data from my xampp server, i need it to be the same.

Below is my code:

public class DHCPInfo extends Activity implements OnClickListener{
public String   s_dns1 ;
public String   s_dns2;     
public String   s_gateway;  
public String   s_ipAddress;        
public String   s_subnet;  
public String   s_serverAddress;


TextView dhcp_info,ip_address;

Button btn_dhcp;
@SuppressWarnings("deprecation")
DhcpInfo dhcpInfo;
WifiManager wifii;

final Handler h = new Handler();

/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dhcp);



    wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE);
    dhcpInfo=wifii.getDhcpInfo();

    s_dns1="DNS 1: "+String.valueOf(dhcpInfo.dns1);
    s_dns2="DNS 2: "+String.valueOf(dhcpInfo.dns2);    
    s_gateway="Default Gateway: "+String.valueOf(dhcpInfo.gateway);    
    s_ipAddress="IP Address: "+String.valueOf(dhcpInfo.ipAddress);     
    s_subnet="Subnet Mask: "+String.valueOf(dhcpInfo.netmask);    
    s_serverAddress="Server IP: "+String.valueOf(dhcpInfo.serverAddress);




    int ipAddress = dhcpInfo.ipAddress;
    intToIpAddress(ipAddress);



    //display them
    dhcp_info= (TextView) findViewById(R.id.text_dhcp);
    dhcp_info.setText("Network Info\n"+s_dns1+"\n"+s_dns2+"\n"+s_gateway+"\n"+s_ipAddress+"\n"+s_serverAddress);

    ip_address= (TextView) findViewById(R.id.text_ip);
    dhcp_info.setText("IP Address\n"+ intToIpAddress(ipAddress));

    btn_dhcp = (Button) findViewById(R.id.btn_dhcp);
    btn_dhcp.setOnClickListener(this);


}

 public String intToIpAddress(int ipAddress) {
    return  ((ipAddress & 0xFF) + "." + 
            ((ipAddress >>>= 8) & 0xFF) + "." + 
            ((ipAddress >>>= 8) & 0xFF) + "." + 
            ((ipAddress >>>= 8) & 0xFF));
}
}

解决方案

As far as I understand you have a laptop and a mobile device. Both are connected to the same WiFi network and your laptop is working as a web server. It is normal that they have different IP addresses - since they are different devices after all. :)

If your router is giving static IPs to the devices (that's unlikely) you could just hardcode the address of your laptop in the Java code. This is more or less OK for debugging but useless for production - since application will work only when you have a device connected to your present WiFi network.

Alternatively, you could make your web application accessible on Internet (instead of just using the local network). If your ISP is giving you a static IP, you could host the application on your laptop. Otherwise you could use some hosting provider - some cloud service should be an OK solution too and pretty cheap.

这篇关于getDhcpInfo获得android系统中错误的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆