我正在用 Java 为 Android 构建一个应用程序。该应用程序可以与传感器设备通信,从设备接收记录的数据并将其发送到 Azure IoT 中心。 但是,当我构建要发送到 Azure 的消息时,消息的大小比我尝试发送的实际数据记录大三倍多。 设备中的一份记录约为 370 kb,而构造为发送到 Azure 的消息最终约为 1500 kb。 我按照此示例创建了将数据发送到 Azure 的代码部分:发送事件示例
在将记录的数据发送到Azure之前,我构造了一个json对象,因为我还想将数据保存在本地作为备份,以防Android无法与Azure建立连接。我希望发送到 Azure 的数据(录音、录音时间、messageId、deviceId 等)与本地保存的数据相同,以便稍后在必要时进行匹配。
这是我用于构建在发送给 Azure 的消息正文中设置的 JSON 对象的代码:
public String createJson(ArrayList recSignal, String deviceId, String smartphoneId, int batterylevel) throws JSONException, UnsupportedEncodingException {
JSONArray SysProp = new JSONArray();
SysProp.put("SystemProperties");
JSONObject SysPropValues = new JSONObject();
try {
SysPropValues.put("messageId", UUID.randomUUID().toString());
SysPropValues.put("correlationId", "???");
SysPropValues.put("connectionDeviceId", smartphoneId); //<-- name of smartphone device in the cloud
SysPropValues.put("contentType", "UTF-8");
SysPropValues.put("enqueuedTime", Instant.now().toString());
} catch (
JSONException e) {
throw new JSONException(e);
}
SysProp.put(SysPropValues);
JSONArray DeviceProp = new JSONArray();
DeviceProp.put("DeviceProperties");
JSONObject DevicePropValues = new JSONObject();
try {
DevicePropValues.put("deviceId", deviceId); //<-- MAC of the connected device
DevicePropValues.put("deviceBatteryLevel", batterylevel);
} catch (JSONException e) {
throw new JSONException(e);
}
DeviceProp.put(DevicePropValues);
JSONObject RecordingBody = new JSONObject();
RecordingBody.put("Body", recSignal);
JSONArray jsonmessage = new JSONArray(); //<-- json to be send to azure
jsonmessage.put(SysProp);
jsonmessage.put(DeviceProp);
jsonmessage.put(RecordingBody);
jsonmessagestring = jsonmessage.toString();
Log.i("Service", "Size of JSON as string is: " + jsonmessagestring.getBytes("UTF-8").length/1000 + "kb");
// *** HERE THE SIZE OF THE JSON OBJECT IS AROUND 370 KB ***
return jsonmessagestring;
}
这是我发送到Azure的代码。这是取自前面提到的示例。
private boolean connectAndSend(String dataToSend, String connString, int numRepeatMessageSend) throws IOException, URISyntaxException, InterruptedException, IotHubClientException {
DeviceClient client = new DeviceClient(connString, protocol);
client.setMessageCallback(new MessageCallbackMqtt(), null);
client.setConnectionStatusChangeCallback(new IotHubConnectionStatusChangeCallbackLogger(), new Object());
Log.i("Service", "Successfully created an IoT Hub client with callback settings.");
client.open(true);
Log.i("Service", "Opened connection to IoT Hub.");
// send message to azure
Log.i("Service", "Constructing message to send to Azure");
Message msg = new Message(dataToSend);
msg.setContentType("application/json;charset=utf-8");
boolean messageSend = false;
Log.i("Service", "Size of message to send to azure is: " + Arrays.toString(msg.getBytes()).length()/1000 + "kb");
// *** HERE THE SIZE OF THE MESSSAGE IS AROUND 1500 KB ***
for (int i = 0; i < numRepeatMessageSend; i++) {
try {
client.sendEvent(msg, D2C_MESSAGE_TIMEOUT_MILLISECONDS);
messageSend = true;
Log.i("Service", "Successfully send message to Azure");
} catch (IotHubClientException e) {
Log.i("Service", "Failed to send message to Azure. Status code: " + e.getStatusCode());
}
}
// close the connection to client
Log.i(TAG, "Closing client");
client.close();
return messageSend;
}
我在here阅读了有关Azure IoT hub消息的消息大小的信息,最终大小是正文大小、消息系统属性值和用户属性值的总和,所以我知道发送到Azure的实际消息将大于我试图发送的数据体,但我不明白为什么它最终会变得这么大。
如果有人可以向我解释原因或有减少消息大小的建议,我们将不胜感激。
修改我的消息大小之前。
public String createJson(ArrayList recSignal, String deviceId, String smartphoneId, int batterylevel) throws JSONException, UnsupportedEncodingException {
JSONObject messageObject = new JSONObject();
try {
messageObject.put("messageId", UUID.randomUUID().toString());
messageObject.put("correlationId", "???");
messageObject.put("connectionDeviceId", smartphoneId);
messageObject.put("contentType", "UTF-8");
messageObject.put("enqueuedTime", Instant.now().toString());
messageObject.put("deviceId", deviceId);
messageObject.put("deviceBatteryLevel", batterylevel);
messageObject.put("Body", recSignal);
} catch (JSONException e) {
throw new JSONException(e);
}
String jsonMessage = messageObject.toString();
Log.i("Service", "Size of JSON as string is: " + jsonMessage.getBytes("UTF-8").length / 1000 + "kb");
// *** NOW THE SIZE OF THE JSON OBJECT SHOULD BE SMALLER ***
return jsonMessage;
}