Android NSD主机返回NULL

时间:2017-09-27 02:28:15

标签: android wifi android-networking

我目前正在使用网络服务发现来检测本地网络上的HTTP服务。我使用了谷歌Android NSDChat项目示例,它只返回主机名,但主机IP地址为NULL。

这是我的函数,它将返回主机名

    public void onServiceFound(NsdServiceInfo service) {

        Log.d(TAG, "Service discovery success" + service.getHost());

        //pref.putString("name", service.getServiceName());
        if (!service.getServiceType().equals(SERVICE_TYPE)) {
            Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
        } else if (service.getServiceName().equals(mServiceName)) {
            Log.d(TAG, "Same machine: " + mServiceName);
        } else if (service.getServiceName().contains(mServiceName)){
            mNsdManager.resolveService(service, mResolveListener);
        }


    }


public void initializeResolveListener() {

mResolveListener = new NsdManager.ResolveListener() {

    @Override
    public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
        // Called when the resolve fails.  Use the error code to debug.
        Log.e(TAG, "Resolve failed" + errorCode);
    }

    @Override
    public void onServiceResolved(NsdServiceInfo serviceInfo) {
        Log.e(TAG, "Resolve Succeeded. " + serviceInfo);

        if (serviceInfo.getServiceName().equals(mServiceName)) {
            Log.d(TAG, "Same IP.");
            return;
        }
        mService = serviceInfo;
        int port = mService.getPort();
        host = mService.getHost(); // getHost() will work now
        Log.d(TAG, "Service discovery success" + host );

    }
};
}

1 个答案:

答案 0 :(得分:0)

我遇到类似的问题,我找到了这个网站。但是在尝试之后,它并没有起作用。

Host is null in NsdServiceInfo of NsdManager.DiscoveryListener.onServiceFound

相关问题