Android NSD主机返回NULL

3

我目前正在使用网络服务发现(Network Service Discovery)来检测本地网络上的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 );

    }
};
}
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接