I send a GET http request to a server and get following data back.
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: 2018年11月16日 16:30:05 GMT
Connection: close
35
{
"uuid" : "13b29524-7e12-48fb-bbf9-4396aefff45d"
}
0
with the following code I get the header info and the body.
void parseResponse() {
boolean headerEnd = false;
byte lineCount = 0;
String line;
char endOfLine[] = "\r\n";
while (1) {
if (client.available()) {
char inChar = client.read();
if (!headerEnd && line.endsWith(endOfLine)) {
// Just save the first then headers
if (lineCount == 10) {
headerEnd = true;
} else {
headers[lineCount] = line;
line = "";
lineCount++;
}
}
if (line.endsWith("close")) {
line = "";
headerEnd = true;
}
if (headerEnd && line.endsWith("}")) {
line.toCharArray(response, sizeof(response));
client.stop();
break;
}
line += inChar;
}
}
}
this thakes 60 - 70ms to parse. Is there a faster way to parse? I think that's slow, for so little text, in the world of AVRs.
1 Answer 1
there are nice functions inherited from Stream class
int parseResponse(char uuid[], int size) {
if (!client.find("HTTP/1.1")) // skip HTTP/1.1
return -1;
int st = client.parseInt(); // parse status code
int l = -1;
if (st == 200 && client.find("\"uuid\" : \"")) {
int l = client.readBytesUntil('"', uuid, size);
uuid[l] = 0; // terminate the C string
}
return l;
}
answered Nov 17, 2018 at 9:14
-
Thanks for the note "client.readBytesUntil". I did not find that under Arduino Client. arduino.cc/en/Reference/ClientConstructor but there, arduino.cc/en/Reference/StreamReadBytesUntil. :S. It's now 20ms faster!Mutus– Mutus2018年11月17日 10:04:34 +00:00Commented Nov 17, 2018 at 10:04
boolean
andbyte
forbool
andunsigned char
respectively. Please retag your question.char lastChar = "";
should bechar lastChar = '';
.""
is a string, not a char.lastChar
variable is never used for anything, why do you keep assigning it?