2016-07-06 69 views
0

我正在編寫一個Android應用程序,需要EventSource才能接收服務器發送的事件。我正在嘗試創建SseEventSourceFactory對象,但創建時會出現NoSuchElement異常。我怎樣才能解決這個問題?爲Android實現Kaazing EventSource

這裏是我的代碼:

// Import java.net classes 
import java.net.URI; 
import java.net.URL; 

// Import EventSource API classes 
import com.kaazing.net.sse.SseEventReader; 
import com.kaazing.net.sse.SseEventSource; 
import com.kaazing.net.sse.SseEventSourceFactory; 
import com.kaazing.net.sse.SseEventType; 

try { 
      // Create Event Source factory(Exception thrown here 
      SseEventSourceFactory factory = SseEventSourceFactory.createEventSourceFactory(); 

      // Create a target location using the java.net.URI create() method 
      final SseEventSource es = factory.createEventSource(URI.create("http://cak9c.com/sse.php")); 

      // Connect to the event source. 
      es.connect(); 

      final Object o = this; 
      Thread sseEventReaderThread = new Thread() { 
       public void run() { 
        try { 
         SseEventReader reader = es.getEventReader(); // Receive event stream 

         SseEventType type = null; 
         while ((type = reader.next()) != SseEventType.EOS) { // Wait until type is DATA 
          switch (type) { 
           case DATA: 
            // Return the payload of the last received event 
            System.out.println(reader.getData()); 
            break; 
           case EMPTY: 
            System.out.println("no data"); 
            break; 
          } 
         } 

        } 
        catch (Exception ex) { 
         System.out.println(ex.getMessage()); 
        } 
       } 
      }; 
      es.close(); 
     } 
     catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
      Toast.makeText(this, e.getLocalizedMessage() + ": " + e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 

回答

-1

請使用PHP代碼:

<?php 

$tickets = [ 
    [ "GOOG", 533.37 ], 
    [ "MSFT", 47.59 ], 
    [ "IBM", 162.99 ], 
    [ "AAPL", 114.12 ], 
    [ "MSFT", 47.29 ], 
]; 

$ticketsLength = count($tickets); 

header("Content-Type: text/event-stream"); 
header("Cache-Control: no-cache"); 
header("Connection: keep-alive"); 

$lastId = 0; 

// Check that lastId is not larger than the size of array - if it is larger star from zero. 
if ($lastId >= $ticketsLength) { 
    $lastId = 0; 
} 

while (true) { 

    sendMessage($lastId, $tickets[$lastId][0], $tickets[$lastId][1]); 
    $lastId++; 

    // Check that lastId is not larger than the size of array - if it is larger close connection. 
    if ($lastId >= $ticketsLength) { 
     die(); 
    } 
    sleep(1); 
} 


function sendMessage($id, $ticket, $price) { 
    echo "id: $id\n"; 
    echo "data: $ticket:$price\n\n"; 
    ob_flush(); 
    flush(); 
} 

?> 

添加下列庫到Android Studio項目:

com.kaazing.gateway.client.android.jar 
com.kaazing.gateway.jms.client.android.jar 
com.kaazing.gateway.jms.client.android-javadoc.jar 

並使用此Java的Android代碼(根據需要更改IP地址)

import java.net.URI; 
import com.kaazing.net.sse.SseEventReader; 
import com.kaazing.net.sse.SseEventSource; 
import com.kaazing.net.sse.SseEventSourceFactory; 
import com.kaazing.net.sse.SseEventType; 


/* 
... 

*/ 


    Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Thread thread = new Thread(new Runnable() 
       { 
        @Override 
        public void run() 
        { 
         try { 
          SseEventSourceFactory factory = SseEventSourceFactory.createEventSourceFactory(); 
          final SseEventSource es = factory.createEventSource(URI.create("http://10.2.56.21/demo.php")); 
          es.connect(); 
          SseEventReader reader = es.getEventReader(); // Receive event stream 

          SseEventType type = null; 
          while ((type = reader.next()) != SseEventType.EOS) { // Wait until type is DATA 
           switch (type) { 
            case DATA: 
             // Return the payload of the last received event 
             Log.e("SOME_TAG", (String) reader.getData()); 
             break; 
            case EMPTY: 
             Log.e("SOME_TAG", "no data"); 
             break; 
           } 

          } 

         } 

         catch(Exception e) 
          { 
           Log.e("SOME_TAG", e.getMessage()); 

          } 
         } 

        }); 

      thread.start(); 

      } 
     }); 

你應該得到的控制檯輸出象下面這樣:

id: 0 
data: GOOG:533.37 

id: 1 
data: MSFT:47.59 

id: 2 
data: IBM:162.99