2013-06-06 56 views
4

我試圖關注來自developer.android.com的網絡教程時遇到問題。
http://developer.android.com/training/connect-devices-wirelessly/nsd.html
我收到一個錯誤,有時我的手機剛關閉。網絡服務發現android教程錯誤:服務丟失,電話關閉

下面是我的代碼:

package com.example.networking; 

import java.io.IOException; 
import java.net.ServerSocket; 

import javax.sound.sampled.Port; 

import android.net.nsd.NsdManager; 
import android.net.nsd.NsdManager.DiscoveryListener; 
import android.net.nsd.NsdManager.RegistrationListener; 
import android.net.nsd.NsdManager.ResolveListener; 
import android.net.nsd.NsdServiceInfo; 
import android.os.Bundle; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Context; 
import android.util.Log; 
import android.view.Menu; 

@SuppressLint("NewApi") 
public class MainActivity extends Activity { 

RegistrationListener mRegistrationListener; 
    DiscoveryListener mDiscoveryListener; 

    String mServiceName; 
    NsdServiceInfo mServiceInfo; 
    ServerSocket mServerSocket; 
    int mLocalPort; 

    NsdManager mNsdManager; 

    final String TAG = "---Networking"; 
    final String SERVICE_TYPE = "_http._tcp."; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Initialize a server socket on the next available port. 
     try { 
      mServerSocket = new ServerSocket(0); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     // Store the chosen port. 
     mLocalPort = mServerSocket.getLocalPort(); 

     registerService(mLocalPort); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public void registerService(int port) { 
     // Create the NsdServiceInfo object, and populate it. 
     mServiceInfo = new NsdServiceInfo(); 

     // The name is subject to change based on conflicts 
     // with other services advertised on the same network. 
     mServiceInfo.setServiceName("NsdChat"); 
     mServiceInfo.setServiceType("_http._tcp."); 
     mServiceInfo.setPort(port); 

     mNsdManager = (NsdManager) getApplicationContext().getSystemService(Context.NSD_SERVICE); 

     initializeRegistrationListener(); 
     initializeDiscoveryListener(); 

     mNsdManager.registerService(
       mServiceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener); 

     mNsdManager.discoverServices(
       SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener); 
    } 

    public void initializeRegistrationListener() { 
     mRegistrationListener = new NsdManager.RegistrationListener() { 

      @Override 
      public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) { 
       // Save the service name. Android may have changed it in order to 
       // resolve a conflict, so update the name you initially requested 
       // with the name Android actually used. 
       mServiceName = NsdServiceInfo.getServiceName(); 
       Log.d(TAG , "Service name: " + mServiceName); 
       Log.d(TAG , "Port number: " + mLocalPort); 
      } 

      @Override 
      public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { 
       // Registration failed! Put debugging code here to determine why. 
       Log.d(TAG , "Registration Failed! Error code: " + errorCode); 
      } 

      @Override 
      public void onServiceUnregistered(NsdServiceInfo arg0) { 
       // Service has been unregistered. This only happens when you call 
       // NsdManager.unregisterService() and pass in this listener. 
       Log.d(TAG , "Service unregistered."); 
      } 

      @Override 
      public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { 
       // Unregistration failed. Put debugging code here to determine why. 
       Log.d(TAG , "Unregistration failed!"); 
      } 
     }; 
    } 

    public void initializeDiscoveryListener() { 

     // Instantiate a new DiscoveryListener 
     mDiscoveryListener = new NsdManager.DiscoveryListener() { 

      // Called as soon as service discovery begins. 
      @Override 
      public void onDiscoveryStarted(String regType) { 
       Log.d(TAG , "Service discovery started"); 
      } 

      @Override 
      public void onServiceFound(NsdServiceInfo service) { 
       // A service was found! Do something with it. 
       Log.d(TAG, "Service discovery success: " + service); 
       if (!service.getServiceType().equals(SERVICE_TYPE)) { 
        // Service type is the string containing the protocol and 
        // transport layer for this service. 
        Log.d(TAG, "Unknown Service Type: " + service.getServiceType()); 
       } else if (service.getServiceName().equals(mServiceName)) { 
        // The name of the service tells the user what they'd be 
        // connecting to. It could be "Bob's Chat App". 
        Log.d(TAG, "Same machine: " + mServiceName); 
       } else if (service.getServiceName().contains("NsdChat")){ 
        mNsdManager.resolveService(service, new ResolveListener() { 

         @Override 
         public void onServiceResolved(NsdServiceInfo serviceInfo) { 
          // TODO Auto-generated method stub 
          Log.d(TAG, "Resolving service..."); 
         } 

         @Override 
         public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { 
          // TODO Auto-generated method stub 
          Log.d(TAG, "Service resolve failed!"); 
         } 
        }); 
       } 
      } 

      @Override 
      public void onServiceLost(NsdServiceInfo service) { 
       // When the network service is no longer available. 
       // Internal bookkeeping code goes here. 
       Log.e(TAG, "service lost: " + service); 
      } 

      @Override 
      public void onDiscoveryStopped(String serviceType) { 
       Log.i(TAG, "Discovery stopped: " + serviceType); 
      } 

      @Override 
      public void onStartDiscoveryFailed(String serviceType, int errorCode) { 
       Log.e(TAG, "Discovery failed: Error code: " + errorCode); 
       mNsdManager.stopServiceDiscovery(this); 
      } 

      @Override 
      public void onStopDiscoveryFailed(String serviceType, int errorCode) { 
       Log.e(TAG, "Discovery failed: Error code: " + errorCode); 
       mNsdManager.stopServiceDiscovery(this); 
      } 
     }; 
    } 
} 

的問題是附加的圖片可見(我不知道我應該怎麼真正稱呼它)。我重複犯錯。

enter image description here

有人能解釋我什麼我做錯了或者我爲什麼得到這個錯誤? 我應該有不同的端口進行註冊和發現嗎? (不同的NsdManager?)

+1

我也陷入了同樣的問題。你找到任何解決方案,如果有的話,請分享.... – shujatAli

回答

0

你的日誌看起來不錯。有成功的發現和成功的解決,所以繼續解決結果。