2013-08-02 47 views
1

我是android编程的初学者。我尝试使用下面的代码播出Wi-Fi直连的消息:用android直播在android中广播

公共类FileTransferService扩展IntentService {

public static final String host= "255.255.255.255"; 
    InetAddress broadcastAddress = InetAddress.getByName(host);// Exception: Unknown host exception 
    int port = 8888; 

     protected void onHandleIntent(Intent intent) { 

    Log.d(WiFiDirectActivity.TAG,"m in 1"); 
    Context context = getApplicationContext(); 

    DatagramSocket socket; 
    try { 
     socket = new DatagramSocket(port); 

     socket.setBroadcast(true); 

      socket.connect(broadcastAddress, port); 
      String message = "Hello"; 
      byte[] buffer = message.getBytes(); 
      DatagramPacket packet = new DatagramPacket(
      buffer, buffer.length, broadcastAddress, port); 
      socket.send(packet); // <----- Causes a SocketException 
    } catch (IOException e) { 
      Log.d(WiFiDirectActivity.TAG, e.getMessage(), e); 
    } 
      } 
     } 

它显示我的GetByName方法()方法未知的主机异常。反正有替代方法吗?我正走在一条正确的道路上吗?我是否需要添加任何内容以发送消息。

在此先感谢

+0

原谅我@ user2645907但Android的API正在使用? Android 4.x?或以下? – gumuruh

回答

0

尝试调用public UnknownHostException (String detailMessage)得到detailed exception消息。

另一种方式来调用GetByName方法()可以从here

以下链接获得具有一步步建立一个Wi-Fi直连广播的步骤示意图 Connecting with Wi-Fi Direct