使用Android通过LAN发现本地服务器连接

时间:2013-12-16 19:04:37

标签: android apache networking tomcat7 jmdns

我试图通过我的android设备发现在http://localhost:8080上运行的本地服务器(apache tomcat)。为了实现这一点,我想到了使用来自heere的jmdns库

但我真的很困惑如何继续,因为我不太了解网络。

这里的代码是用谷歌搜索写的,但任何帮助都会有很大的帮助。

public class DnssdDiscovery extends Activity {

protected static final String TAG = DnssdDiscovery.class.getSimpleName();
android.net.wifi.WifiManager.MulticastLock lock;
android.os.Handler handler = new android.os.Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    handler.postDelayed(new Runnable() {
        public void run() {
            setUp();
        }
        }, 1000);

}    /** Called when the activity is first created. */



//  private String type = "_workstation._tcp.local.";_http._tcp.local.
private String type = "_http._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 {
        InetAddress Address = InetAddress.getLocalHost(); 
        Log.e("Local :", Address.getHostName());
        jmdns = JmDNS.create(Address);
        jmdns.addServiceListener(type, listener = new ServiceListener() {

            @Override
            public void serviceResolved(ServiceEvent ev) {
                notifyUser("Service resolved: " + ev.getInfo().getQualifiedName() + " port:" + ev.getInfo().getPort());
            }

            @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("_http._tcp.local.", "AndroidTest", 8080, "plain test service from android");
        jmdns.registerService(serviceInfo);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
}


private void notifyUser(final String msg) {
    handler.postDelayed(new Runnable() {
        public void run() {

    TextView t = (TextView)findViewById(R.id.text);
    t.setText(msg+"\n=== "+t.getText());
        }
        }, 1);

}

@Override
    protected void onStart() {
    super.onStart();
    //new Thread(){public void run() {setUp();}}.start();
}

@Override
    protected void onStop() {
    if (jmdns != null) {
        if (listener != null) {
            jmdns.removeServiceListener(type, listener);
            listener = null;
        }
        jmdns.unregisterAllServices();
        try {
            jmdns.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        jmdns = null;
    }
    //repo.stop();
    //s.stop();
    lock.release();
    super.onStop();
}

}

0 个答案:

没有答案