简体   繁体   中英

Accses to data from IoT Hub Azure with Java

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.

  1. Simple send/receive sample : Shows how to connect then send and receive messages to and from IoT Hub, passing the protocol of your choices as a parameter.
  2. Simple sample handling messages received : : Shows how to connect to IoT Hub and manage messages received from IoT Hub, passing the protocol of your choices as a parameter.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM