0
我目前使用網絡服務發現來檢測本地網絡上的HTTP服務。我使用了Google Android NSDChat項目示例,它只向我返回主機名,但主機IP地址爲NULL。Android NSD主機返回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);
}
};
}