我正在使用Adafruit Feather Huzzah ESP8266和ArduinoJson library来解析HTTP请求的响应。我已经成功获得了预期的响应,但是反序列化过程每隔一次失败 ,它遍历循环。我认为这可能是内存分配问题,但我无法解决问题。任何帮助将不胜感激。
我尝试过使用动态文档还是静态文档,并在循环内/循环外初始化它们而没有成功。我也尝试了doc.clear()方法来释放内存,但是仍然没有运气。下面是我的循环,缺少连接参数:
void loop() {
WiFiClientSecure client;
for (int i = 0; i <= numOfInstallations - 1; i++) {
String url = "/v2/installations/" + String(idSites[i]) +
"/widgets/Graph?attributeCodes[]=SOC&instance=" + String(instance[i]) +
"&start=" + String(startTime) +
"&end=" + String(endTime);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"X-Authorization: Token " + token + "\r\n" +
"Connection: keep-alive\r\n\r\n");
Serial.println(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"X-Authorization: Token " + token + "\r\n" +
"Connection: keep-alive\r\n\r\n");
Serial.println("request sent");
delay(500);
// Ignore the response headers
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders)) {
Serial.println(F("No headers"));
return;
}
const size_t capacity = 5*JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(5) + 2*JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) + 130;
DynamicJsonDocument doc(capacity); // Json document setup
// Get the Json data from the response
DeserializationError error = deserializeJson(doc, client);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
Serial.print("\r\n");
}
else {
Serial.println("deserializeJson() successful\r\n");
}
}
}
我希望每次反序列化过程都能成功,但这是输出:
GET /v2/installations/xxxxx/widgets/Graph?attributeCodes[]=SOC&instance=215&start=1555070100&end=1555070400 HTTP/1.1
Host: vrmapi.victronenergy.com
X-Authorization: Token c324f8876e672ad1797cd69a9d9f62611507d25aa5a0b1ff40f9fb524d96f2fc
Connection: keep-alive
request sent
deserializeJson() successful
GET /v2/installations/xxxxx/widgets/Graph?attributeCodes[]=SOC&instance=258&start=1555070100&end=1555070400 HTTP/1.1
Host: vrmapi.victronenergy.com
X-Authorization: Token XXXXXXXXXX
Connection: keep-alive
request sent
deserializeJson() failed: InvalidInput
GET /v2/installations/xxxxx/widgets/Graph?attributeCodes[]=SOC&instance=258&start=1555070100&end=1555070400 HTTP/1.1
Host: vrmapi.victronenergy.com
X-Authorization: Token XXXXXXXXXX
Connection: keep-alive
request sent
deserializeJson() successful
GET /v2/installations/xxxxx/widgets/Graph?attributeCodes[]=SOC&instance=258&start=1555070100&end=1555070400 HTTP/1.1
Host: vrmapi.victronenergy.com
X-Authorization: Token XXXXXXXXXX
Connection: keep-alive
request sent
deserializeJson() failed: InvalidInput
GET /v2/installations/xxxxx/widgets/Graph?attributeCodes[]=SOC&instance=258&start=1555070100&end=1555070400 HTTP/1.1
Host: vrmapi.victronenergy.com
X-Authorization: Token XXXXXXXXXX
Connection: keep-alive
request sent
deserializeJson() successful