I send data to the IoT Hub and receive it, it works, but i dont know how i can work with the received Data: here is my Code to receive data:
public void accept(PartitionReceiver receiver)
{
System.out.println("** Created receiver on partition " + partitionId);
try {
while (true) {
Iterable<EventData> receivedEvents = receiver.receive(10).get();
int batchSize = 0;
if (receivedEvents != null)
{
for(EventData receivedEvent: receivedEvents)
{
System.out.println(String.format("| Time: %s", receivedEvent.getSystemProperties().getEnqueuedTime()));
System.out.println(String.format("| Device ID: %s", receivedEvent.getProperties().get("iothub-connection-device-id")));
System.out.println(String.format("| Message Payload: %s", new String(receivedEvent.getBody(), Charset.defaultCharset())));
batchSize++;
}
}
}
}
catch (Exception e)
{
System.out.println("Failed to receive messages: " + e.getMessage());
}
}
I would like to work with the received data, here I become the data as JSON String:
System.out.println(String.format("| Message Payload: %s", new String(receivedEvent.getBody(), Charset.defaultCharset())));
The dataoutput is: product: xy, price: 2.3. I would like take the data to :
String product= product;
double price= price;
How can I the received Payload save in the variable?
Thanks
There are two kinds of messages which include device-to-cloud
and cloud-to-device
.
For the first kind, device-to-cloud
messages, as @DominicBetts said, you can refer to the section Receive device-to-cloud messages
to know how to receive d2c messages with Event Hub-compatible endpoint. And there are two samples as references on GitHub, please see below.
For the second kind, cloud-to-device
messages, you can refer to the section Receiving messages on the simulated device
to know how to receive c2d messages. The sample code in the article was writen for C#, but I think it's simple for using Java instead of C#, please notice the note in the section for choosing the suitable protocol.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.