如何从Java / Android中自动获取笔记本电脑的IP地址

时间:2018-07-18 22:54:22

标签: java android gradle android-gradle

我有一个带服务器的Android WebView和一台笔记本电脑。如何使用Java for Android WebView 自动获取笔记本电脑的IP地址?

当前,我必须手动查找笔记本电脑的IP地址,并将其键入到我的应用代码中,如下所示:webView.loadUrl("http://10.0.2.1/mylinks/");

如何自动设置该IP地址?

2 个答案:

答案 0 :(得分:1)

您可以在构建时通过将以下内容添加到应用程序的build.gradle脚本中,将开发计算机的当前IP地址保存到Java常量中。

添加到SELECT Id FROM Test WHERE (SUBSTRING(SUBSTRING_INDEX(code,':',1),2)=FileNum) and (SUBSTRING(SUBSTRING_INDEX(code,':',-1),1)>=LowLim and (SUBSTRING(SUBSTRING_INDEX(code,':',-1),1)<=HighLim) 部分:

defaultConfig

然后在android { defaultConfig { //Set BuildConfig.DEVELOPER_MACHINE_IP to the IP address of any interface. def ipList = getLocalIPv4() buildConfigField("String", "DEVELOPER_MACHINE_IP", ipList.empty ? '""' : '"' + ipList.get(0) + '"') } } 部分之后的build.gradle底部添加以下方法:

dependencies

从构建配置变量//return all v4 addresses static def getLocalIPv4() { def ip4s = [] NetworkInterface.getNetworkInterfaces() .findAll { it.isUp() && !it.isLoopback() && !it.isVirtual() } .each { it.getInetAddresses() .findAll { !it.isLoopbackAddress() && it instanceof Inet4Address } .each { ip4s << it.getHostAddress() } } return ip4s }

中获取程序中任何位置的IP地址。
BuildConfig.DEVELOPER_MACHINE_IP

对于您的网络视图,您将使用以下内容:

package com.test.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText( this,"The developer machine is " + BuildConfig.DEVELOPER_MACHINE_IP, Toast.LENGTH_LONG).show();
    }
}

答案 1 :(得分:0)

以下代码遍历它具有的所有接口,然后打印该接口的IPv4,IPv6和mac地址。对于LAN IP地址,您可以使用功能 isSiteLocal(),如果IP地址是本地地址,则返回true。

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class App{

public static void main(String[] args)throws Exception {
    // getting the list of interfaces in the local machine
    Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
    while( n.hasMoreElements()){ //for each interface
        System.out.println("----------------------------------------------------");
        NetworkInterface e = n.nextElement();
    //name of the interface
        System.out.println("Interface Name: " + e.getName());
    /* A interface may be binded to many IP addresses like IPv4 and IPv6
        hence getting the Enumeration of list of IP addresses  */
        Enumeration<InetAddress> a = e.getInetAddresses();
        while( a.hasMoreElements()){
            InetAddress addr = a.nextElement();
            String add = addr.getHostAddress().toString();
            if( add.length() < 17 )
                System.out.println("IPv4 Address: " + add);
            else
                System.out.println("IPv6 Address: " + add);
        }
        if(e.getHardwareAddress() != null){
                    // getting the mac address of the particular network interface
            byte[] mac = e.getHardwareAddress();
                    // properly formatting the mac address
            StringBuilder macAddress = new StringBuilder();
            for(int i =0; i < mac.length; i++){
                macAddress.append(String.format("%03X%s", mac[i],(i < mac.length -1) ? "-":""));
            }
            System.out.println("Hardware adrress: " + macAddress.toString());
        }
        System.out.println("----------------------------------------------------");
    }
}

}

kali linux 2.0中的代码输出为:
-------------------------------------------------- -
接口名称:wlan0
IPv6地址:fe80:0:0:0:1992:d9bc:7d8c:d85b%wlan0
IPv4地址:192.168.1.137
硬件地址:078-0E4-000-0E7-0B0-046
-------------------------------------------------- -
-------------------------------------------------- -
接口名称:lo
IPv6地址:0:0:0:0:0:0:0:1:1%lo
IPv4地址:127.0.0.1
-------------------------------------------------- -

相关问题