Внедрение Kaazing EventSource для Android

Я пишу приложение для Android, которое требует EventSource получать отправленные сервером события. Я пытаюсь создать SseEventSourceFactory объект, но я получаю NoSuchElement исключение при создании. Как я могу это исправить?

Вот мой код:

// 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();
}

0

Решение

Пожалуйста, используйте этот код 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
-1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]