1

I'm currently using mqtt to communicate between client and server and mqtt publish method takes message as bytes. I need to send latitude, longitude, address,etc in my single mqtt publish and be able to receive those on server side. How can I achieve it?

I'm using wmqtt client library on client side (android) and paho client library on server side (jsp,servlets).

deviceloc d=new deviceloc();
d.id="1234";
d.add="hyder";
d.lat=17.5;
d.lon=78.5;
try {
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o=new ObjectOutputStream(b);
o.writeObject(d);
byte bytes[]=b.toByteArray();
MqttMessage data=new MqttMessage(bytes);
ByteArrayInputStream b1 = new ByteArrayInputStream(data.toString().getBytes());
ObjectInputStream o1 = new ObjectInputStream(b1);
Object obj1;
try {
obj1 = o1.readObject();
deviceloc dd=(deviceloc)obj1;
System.out.println(dd.id);
System.out.println(dd.add);
System.out.println(dd.lat);
System.out.println(dd.lon);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
catch(IOException e)
{
e.printStackTrace();
}

I'm getting streamcorrupted exception

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
Reva
  • 225
  • 2
  • 6
  • 14

1 Answers1

3

Serialise your objects to xml (or CSV or Json or roll-your-own format) strings. Form your message from those strings. Send your message as bytes. Reverse the process at the receiving end.

paulkayuk
  • 1,052
  • 1
  • 8
  • 15
  • Hi paulkayuk, deviceloc d=new deviceloc(); d.id=mqttClientId; d.add=strAddress; d.lat=loc.getLatitude(); d.lon=loc.getLongitude(); ByteArrayOutputStream b = new ByteArrayOutputStream(); ObjectOutputStream o=new ObjectOutputStream(b); o.writeObject(d); byte bytes[]=b.toByteArray(); mqttClient.publish("admin/location",bytes,MQTT_QUALITY_OF_SERVICE,MQTT_RETAINED_PUBLISH); this is how i send the message. i am deserializing on server side, but i am getting error as clientsidepackagename.deviceloc class not found error. how to rectify that – Reva Feb 27 '14 at 11:07
  • ByteArrayInputStream b1 = new ByteArrayInputStream(data.toString().getBytes()); ObjectInputStream o1 = new ObjectInputStream(b1); deviceloc obj1=(deviceloc)o1.readObject(); System.out.println(obj1.id); System.out.println(obj1.add); System.out.println(obj1.lat); System.out.println(obj1.lon); this is what i do in server side – Reva Feb 27 '14 at 11:13
  • data on second comment is of type MQTTMessage that is received on server side. Thanks for the answer. – Reva Feb 27 '14 at 11:15
  • 1
    I'm not sure you can use Java object serialisation between android and Java since the object internal structures are not necessarily the same between Dalvic and Java. As @paulkayuk suggested I would use a basic text based format to serialise the object. It doesn't have to be XML, CSV or Json would work just as well the relatively simple case you mentioned. (Also you would do better to edit you question to include the code rather than trying to put it in a comment) – hardillb Feb 27 '14 at 13:03
  • @hardillb Thanks for the reply. i tried putting all the code on server side as in above code. and i get StreamCorruptedException. – Reva Feb 27 '14 at 13:31