eventsource-android returns empty data String from Flux server endpoint

Viewed 182

I have successfully implemented this github library for SSE Handling in Android.

The problem

Each time i'm sending data to the stream , the onMessage function is triggered successfully. Although the provided data field of MessageEvent is empty.

enter image description here

My Implementation

Main.java

    private SSEHandler sseHandler = new SSEHandler();
    private EventSource eventSource;

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

        startEventSource();
    }

    private void startEventSource() {
        eventSource = new EventSource.Builder(".../ambulance/assign/receive")
                .eventHandler(sseHandler)
                //  .headers()
                .build();
        eventSource.connect();
    }

    private void stopEventSource() {
        if (eventSource != null)
            eventSource.close();
        sseHandler = null;
    }

SSEHandler.java

public class SSEHandler implements EventSourceHandler {



    public SSEHandler() {}

    @Override
    public void onConnect() {
        Log.e("SSE Connected", "True");
    }

    @Override
    public void onMessage(String event, MessageEvent message) {
        Log.e("SSE Message", event);
        Log.e("SSE Message: ", String.valueOf(message));
    }

    @Override
    public void onComment(String comment) {
        //comments only received if exposeComments turned on
        Log.e("SSE Comment", comment);
    }

    @Override
    public void onError(Throwable t) {
        //ignore ssl NPE on eventSource.close()
        Log.e("SSE Error", "Error");

    }

    @Override
    public void onClosed(boolean willReconnect) {
        Log.e("SSE Closed", "reconnect? " + willReconnect);
    }
}

Server Side Endpoint

@RequestMapping(method = RequestMethod.GET, value = "/ambulance/assign/receive")
public Flux < MessageEvent > addReport() {

 return Flux.create(sink -> {
  assignProcessor.register(sink::next);
 });

}   

No matter what is the return type of Flux <Object> the data field is always empty on android

POST payload & MessageEvent Object

Payload

{
 "data": "test",
 "lastEventId": "tester",
 "origin": "my origin"
}

MessageEvent.java

public class MessageEvent {
 public String data;
 public String lastEventId;
 public String origin;

 public MessageEvent(String data, String lastEventId, String origin) {
  this.data = data;
  this.lastEventId = lastEventId;
  this.origin = origin;
 }

 public MessageEvent(String data) {
  this(data, null, null);
 }

 @Override
 public boolean equals(Object o) {
  if (this == o) return true;
  if (o == null || getClass() != o.getClass()) return false;

  MessageEvent that = (MessageEvent) o;

  if (data != null ? !data.equals(that.data) : that.data != null) return false;
  if (lastEventId != null ? !lastEventId.equals(that.lastEventId) : that.lastEventId != null) return false;
  if (origin != null ? !origin.equals(that.origin) : that.origin != null) return false;

  return true;
 }

 @Override
 public int hashCode() {
  int result = data != null ? data.hashCode() : 0;
  result = 31 * result + (lastEventId != null ? lastEventId.hashCode() : 0);
  result = 31 * result + (origin != null ? origin.hashCode() : 0);
  return result;
 }

 @Override
 public String toString() {
  return "MessageEvent{" +
   "data='" + data + '\'' +
   ", lastEventId='" + lastEventId + '\'' +
   ", origin='" + origin + '\'' +
   '}';
 }
}

Already tried with : Flux <String> data is still empty.

Based on this EventStreamParser I assume that it should parse as data every Object in JSON format because of this function.

 private void processField(String field, String value)

I even tried plain text, but didn't seem to work either...

1 Answers

I managed to read the Object using another java library

This one:

Maven

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.7.2</version>
</dependency>

Gradle

// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.7.2'

Buildr

# https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
'com.squareup.okhttp3:okhttp:jar:4.7.2'

Implemented EventSource as before


public class SimpleEventHandler implements EventHandler {


 @Override
 public void onOpen() throws Exception {
  System.out.println("onOpen");
 }

 @Override
 public void onClosed() throws Exception {
  System.out.println("onClosed");
 }

 @Override
 public void onMessage(String event, MessageEvent messageEvent) throws Exception {
  System.out.println(messageEvent.getData());
 }

 @Override
 public void onComment(String comment) throws Exception {
  System.out.println("onComment");
 }

 @Override
 public void onError(Throwable t) {
  System.out.println("onError: " + t);
 }


}

Main method


public static void main(String[] args) throws InterruptedException, ExecutionException, UnknownHostException, IOException {
  EventHandler eventHandler = new SimpleEventHandler();
  String url = String.format("https://aedproject.herokuapp.com/api/ambulance/assign/receive");
  EventSource.Builder builder = new EventSource.Builder(eventHandler, URI.create(url))
   .reconnectTime(Duration.ofMillis(3000));

  try (EventSource eventSource = builder.build()) {
   eventSource.start();

   TimeUnit.MINUTES.sleep(10);
  } catch (Exception e) {

  }

This time, the data filed of MessageEvent contains my whole Object!

Related