JmDNS错误 - 对于ServiceInfo类型,未定义getInetAddresses()方法

时间:2011-11-16 18:11:23

标签: android jmdns

由于某种原因,它不喜欢行中的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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

查看this SO question了解详情。希望这有帮助。