7

I need to send data from a java app to an android app in real time. The data is not large(integers within 0 and 9) but the number of transmissions is high, around 5 transmissions per second.

I wish to have a publisher subscriber model. Java app is going to push data to the android app instead of android app continuously pinging the java app for data.

Transmission has to be in real time and will happen over a LAN.

Tried with GCM but its not real time. Pubnub is another but not sure how realtime it will be.

How to proceed ?

Soumya
  • 1,833
  • 5
  • 34
  • 45
  • JMS may not be supported in android. Looking at ActiveMQ, any idea ? – Soumya Jun 12 '14 at 05:54
  • Is there a specific reason why you want to push? And "RealTime" is a bit broad. What are the exact realtime-requirements? Have you thought about plain old sockets? – Fildor Jun 12 '14 at 06:26
  • Its like a java app is sending data continuously to an android client app. The data being generated by the java app needs to reach the android app then and there so that some action could be take. A maximum delay of 1 or 2 seconds may be permissible.i don't want the android app to request the java app for data and that is why i want the java app to push the data to android instead of android pulling the data – Soumya Jun 12 '14 at 06:49
  • How about a socket server that supports multiple clients? I have recently built a similar project, although my server application was written in C#. Check out the `java.net.*` libraries. I used `InetSocketAddress`, `Socket`, and `SocketAddress`. – BVB Jun 13 '14 at 01:10

4 Answers4

4

PubNub is as real-time as it gets... and it's super easy to make a Java server talk to Android device (in fact, PubNub has client SDKs for over 50 different platforms!).

This same code will run on both Android and plain Java:

import com.pubnub.api.*;
import org.json.*;

Pubnub pubnub = new Pubnub("demo", "demo");

Callback callback = new Callback() {
  public void successCallback(String channel, Object response) {
      Log.d("PUBNUB",response.toString());
  }
  public void errorCallback(String channel, PubnubError error) {
      Log.d("PUBNUB",error.toString());
  }
};

try {
  pubnub.subscribe("demo", callback);
    } catch (PubnubException e) {
  Log.d("PUBNUB",e.toString());
}

pubnub.publish("demo", "Hello World !!" , callback);

The complete walkthrough is available here:

http://www.pubnub.com/docs/java/javase/tutorial/data-push.html

If you have additional questions about it, feel free to contact support@pubnub.com, and they'll be happy to assist you further.

geremy

Geremy
  • 2,415
  • 1
  • 23
  • 27
0

If you are transmitting data from java to android app in LAN then you can use TCp/UDP for real time data transfer between java and android app. In one of my project i was in same situation but i want to transfer string from android app to java application in pc. In LAN i use TCP. java app was listening for tcp connection and when android app send request to java app for connection then data was also transfer.

you can use this scenario for your problem

yasiriqbal776
  • 406
  • 3
  • 15
0

I would suggest you use this approach:

  1. Make a server script that serves the application, you can use PHP. This is the preferred method, but you can stick with the java server.
  2. Make your server listen on an specified port (if you chose java) or just start the web server if you use the HTTP version.
  3. Your app is who should start the communication, not the other way around.

If you want to go for the HTTP server, you must learn about JSON or any AJAX techniques.

If you stick with the java server you may want to implement something like this:

  1. Your server waits for a connection, e.g. on port 9999.
  2. Your application sends a synchronization package via ObjectOutputStream to let the server know a client wants information.
  3. The server sends the information back through the same socket.
  4. The client repeats this as many times as it needs.

A client might look like this:

public class Client {
    public void retrieveData() {
        ResponseObject response = null;
        try {
            Socket socket = new Socket("host", 9999);
            ObjectOutputStream oos = new ObjectOutputStream( socket.getOutputStream() );
            oos.writeObject(criterio);
            oos.close();
            ObjectInputStream ois = new ObjectInputStream( socket.getInputStream() );
            Response response = (Response) ois.readObject();
            socket.close();
        } catch (IOException e) {
            System.out.println("Error: "+e.getMessage());
        }
        System.out.println("Response was: "response.toString());
    }
}

And a server like this:

public class Server {
    public void startServer() {
        ServerSocket serverSocket = new ServerSocket(9999);
        ExecutorService service = Executors.newFixedThreadPool(MAX_THREADS);

        while (true) {
            try {
                servicio.submit(new Receiver(serverSocket.accept()));
            } catch (IOException e) {
                System.out.println("Error: "+e.getMessage());
            }
        }

        serverSocket.close();
    }

    public class Receiver implements Callable<Object> {
        Socket socket;

        Receiver(Socket socket) {
            this.socket = socket;
        }

        public Object call() {
            ObjectInputStream ois = new ObjectInputStream( socket.getInputStream() );
            Object clientData = ois.readObject();
            ois.close();

            ObjectOutputStream oos = new ObjectOutputStream( socket.getOutputStream() );
            oos.writeObject(...); /* Your response goes here */
            oos.close();

            socket.close();
            return null;
        }
    }
}
arielnmz
  • 8,354
  • 9
  • 38
  • 66
  • If i am not wrong, the client needs to ask for data from server !! I want the server to push the data to the client. – Soumya Jun 12 '14 at 05:51
  • That's why you need to send a `synchronization` signal to let the server know that a client wants data, open a connection and push data to the client through that connection. A server should **never** open a connection to a client. But if you insist, then you would have to use the `Server` class on the client and the `Client` class on the server, and leave a port open on the client. – arielnmz Jun 12 '14 at 06:01
0

Check out MQTT. I've used that for going from ActiveMQ to Android. My Android apps use an MQTT library that connect to ActiveMQ and subscribe to a topic.

Here are some links:

  1. http://activemq.apache.org/mqtt.html
  2. http://www.eclipse.org/paho/
  3. Basic Steps for Using MQTT in android

I believe the client library I used came from here: http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.java.git/

Community
  • 1
  • 1
Kevin Reynolds
  • 421
  • 2
  • 9