【问题标题】:Android NSD Host returns NULLAndroid NSD 主机返回 NULL
【发布时间】:2018-03-08 09:24:03
【问题描述】:

我目前正在使用网络服务发现来检测本地网络上的 HTTP 服务。我使用了 Google 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 );

    }
};
}

【问题讨论】:

    标签: android wifi android-networking


    【解决方案1】:

    我遇到了类似的问题,我找到了这个网站。然而,在尝试之后,它并没有很好地工作。

    Host is null in NsdServiceInfo of NsdManager.DiscoveryListener.onServiceFound

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 2017-04-22
      • 2017-06-20
      • 2012-06-22
      • 2012-05-28
      • 2014-12-05
      • 2021-07-07
      相关资源
      最近更新 更多