JmDNS 错误 - ServiceInfo 类型的 getInetAddresses() 方法未定义

发布于 2024-12-15 20:29:41 字数 2566 浏览 2 评论 0原文

由于某种原因,它不喜欢以下行中的 getInetAddresses() 方法:

Enumeration additions = (Enumeration) ev.getInfo().getInetAddresses();
if (ev.getInfo().getInetAddresses() != null && ev.getInfo().getInetAddresses().length > 0) {
    additions =  ev.getInfo().getInetAddresses()[0].getHostAddress();
}

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import javax.jmdns.ServiceInfo;

private String type = "_workstation._tcp.local.";
private JmDNS jmdns = null;
private ServiceListener listener = null;
private ServiceInfo serviceInfo;
private void setUp() {
    android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
    lock = wifi.createMulticastLock("mylockthereturn");
    lock.setReferenceCounted(true);
    lock.acquire();
    try {
        jmdns = JmDNS.create();
        jmdns.addServiceListener(type, listener = new ServiceListener() {

            @Override
            public void serviceResolved(ServiceEvent ev) {
                Enumeration additions = (Enumeration) ev.getInfo().getInetAddresses();
                if (ev.getInfo().getInetAddresses() != null && ev.getInfo().getInetAddresses().length > 0) {
                    additions =  ev.getInfo().getInetAddresses()[0].getHostAddress();
                }
                notifyUser("Service resolved: " + ev.getInfo().getQualifiedName() + " port:" + ev.getInfo().getPort() + additions);
            }

            @Override
            public void serviceRemoved(ServiceEvent ev) {
                notifyUser("Service removed: " + ev.getName());
            }

            @Override
            public void serviceAdded(ServiceEvent event) {
                // Required to force serviceResolved to be called again (after the first search)
                jmdns.requestServiceInfo(event.getType(), event.getName(), 1);
            }
        });
        serviceInfo = ServiceInfo.create("_test._tcp.local.", "AndroidTest", 0, "plain test service from android");
        jmdns.registerService(serviceInfo);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
}

For some reason it doesn't like the getInetAddresses() method in the lines:

Enumeration additions = (Enumeration) ev.getInfo().getInetAddresses();
if (ev.getInfo().getInetAddresses() != null && ev.getInfo().getInetAddresses().length > 0) {
    additions =  ev.getInfo().getInetAddresses()[0].getHostAddress();
}

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import javax.jmdns.ServiceInfo;

private String type = "_workstation._tcp.local.";
private JmDNS jmdns = null;
private ServiceListener listener = null;
private ServiceInfo serviceInfo;
private void setUp() {
    android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
    lock = wifi.createMulticastLock("mylockthereturn");
    lock.setReferenceCounted(true);
    lock.acquire();
    try {
        jmdns = JmDNS.create();
        jmdns.addServiceListener(type, listener = new ServiceListener() {

            @Override
            public void serviceResolved(ServiceEvent ev) {
                Enumeration additions = (Enumeration) ev.getInfo().getInetAddresses();
                if (ev.getInfo().getInetAddresses() != null && ev.getInfo().getInetAddresses().length > 0) {
                    additions =  ev.getInfo().getInetAddresses()[0].getHostAddress();
                }
                notifyUser("Service resolved: " + ev.getInfo().getQualifiedName() + " port:" + ev.getInfo().getPort() + additions);
            }

            @Override
            public void serviceRemoved(ServiceEvent ev) {
                notifyUser("Service removed: " + ev.getName());
            }

            @Override
            public void serviceAdded(ServiceEvent event) {
                // Required to force serviceResolved to be called again (after the first search)
                jmdns.requestServiceInfo(event.getType(), event.getName(), 1);
            }
        });
        serviceInfo = ServiceInfo.create("_test._tcp.local.", "AndroidTest", 0, "plain test service from android");
        jmdns.registerService(serviceInfo);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-12-22 20:29:41

尝试从 Maven Central Repository 下载并使用最新版本的 JmDNS,显然存在一些相关问题到从 SourceForge 主页下载的 jar 文件。来自 SourceForge 的 jar 文件在 Android 上运行得不太好。

查看这个问题了解更多详细信息。希望这有帮助。

Try download and use latest version of JmDNS from Maven Central Repository, apparently there are some issues related to the jar file downloaded from their SourceForge home page. jar file from SourceForge doesn't play quite nice with Android.

Check out this SO question for more details. Hope this help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文