使用Java访问物联网中心Azure的数据
我向物联网中心发送数据并接收数据,它可以工作,但我不知道如何处理接收到的数据:以下是我接收数据的代码:使用Java访问物联网中心Azure的数据,java,azure,cloud,iot,azure-iot-hub,Java,Azure,Cloud,Iot,Azure Iot Hub,我向物联网中心发送数据并接收数据,它可以工作,但我不知道如何处理接收到的数据:以下是我接收数据的代码: public void accept(PartitionReceiver receiver) { System.out.println("** Created receiver on partition " + partitionId); try { while (t
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());
}
}
数据输出为:产品:xy,价格:2.3。
我想把数据带到:
String product= product;
double price= price;
如何将收到的有效负载保存在变量中
谢谢有两种消息,包括
设备到云
和云到设备
对于第一类,设备到云
消息,正如@DominicBetts所说的,您可以参考本节了解如何使用与事件中心兼容的端点接收d2c消息。GitHub上有两个示例作为参考,请参见下文
对于第二类,
云到设备
消息,您可以参考本节了解如何接收c2d消息。本文中的示例代码是为C#编写的,但我认为使用Java而不是C#很简单,请注意章节中关于选择合适协议的说明。您能提供有关如何接收数据的更多信息吗?您可能会发现这篇文章很有用,它描述了使用JavaSDK向IoT Hub发送和接收数据:它很有效!谢谢,彼得·潘和多米克·贝特。你有什么想法吗?奥菲兹,请看。
String product= product;
double price= price;