解析 GET 响应



我无法从GET响应中提取我需要的数据我只想记住"-"和"-"之间的字符,因为我不需要其他任何东西。我尝试使用如下所示的while循环,但它不起作用

谢谢

    String readString;
    while (client.connected() || client.available()) {
      char c = client.read(); //gets byte from ethernet buffer
      while(c != "-"); //this throw error
      readString += c; //places captured byte in readString
    }
    client.stop(); //stop client
    Serial.print(readString);
    String res = readString.substring("-","-"); //throw error
    String val = "happy";
    if(res == val){
      Serial.print(res);
      Serial.println(" happy");
    }else{
      Serial.print(res);
      Serial.println(" sad");
    }
  Serial.println("==================");
  Serial.println();
  readString=""; //clear readString variable

好的,在这里你有不同的问题。

  1. 您无法比较字符 ( c ) 和字符串 ( "-" )。您应该使用'-'与字符进行比较。
  2. 循环中时将阻止程序执行。您应该使用一种状态机(例如使用状态变量)。
  3. 如果您正确编写了读取函数,则无需使用 substring 函数。在任何情况下,substring函数都需要索引,而不是字符。

也就是说,这段代码应该适用于您的情况:

// state =
// 0 -> waiting for first -
// 1 -> reading the answer and waiting for second -
// 2 -> finished reading
uint8_t state = 0;
while (client.connected() || client.available()) {
    char c = client.read(); //gets byte from ethernet buffer
    if ((c == '-') && (state < 2))
        state++;
    if ((state == 1) && (c != '-'))
        readString += c;
}
client.stop(); //stop client
Serial.print(readString);
String val = "happy";
if(res == val){
    Serial.print(readString);
    Serial.println(" happy");
}else{
    Serial.print(readString);
    Serial.println(" sad");
}

无论如何,我真的不喜欢在arduino中使用可变大小的变量,因为你的内存很少(那你为什么要浪费它呢?我的建议是始终使用固定大小的字符串(又名字符数组)。例如:

// state =
// 0 -> waiting for first -
// 1 -> reading the answer and waiting for second -
// 2 -> finished reading
uint8_t state = 0;
char readString[MAX_SIZE+1];
uint8_t readStringIndex = 0;
while (client.connected() || client.available()) {
    char c = client.read(); //gets byte from ethernet buffer
    if ((c == '-') && (state < 2))
        state++;
    if ((state == 1) && (c != '-'))
    {
        readString[readStringIndex] = c;
        readStringIndex++;
    }
}
readString[readStringIndex] = ''; // string terminator
client.stop(); //stop client
Serial.print(readString);
if(strcmp(readString, "happy")){
    Serial.print(readString);
    Serial.println(" happy");
}else{
    Serial.print(readString);
    Serial.println(" sad");
}

编辑:

OP提到字符串不再由两个"-"分隔,而是括在"<"和">"之间。

因此,应按以下方式修改代码:

// state =
// 0 -> waiting for first -
// 1 -> reading the answer and waiting for second -
// 2 -> finished reading
uint8_t state = 0;
char readString[MAX_SIZE+1];
uint8_t readStringIndex = 0;
while (client.connected() || client.available()) {
    char c = client.read(); //gets byte from ethernet buffer
    switch (state)
    {
        case 0: // Waiting for first char
            if (c == '<')
                state = 1;
            break;
        case 1: // Reading the answer and waiting for second char
            if (c == '>')
                state = 2;
            else
            {
                readString[readStringIndex] = c;
                readStringIndex++;
            }
            break;
    }
}
readString[readStringIndex] = ''; // string terminator
client.stop(); //stop client
Serial.print(readString);
if(strcmp(readString, "happy")){
    Serial.print(readString);
    Serial.println(" happy");
}else{
    Serial.print(readString);
    Serial.println(" sad");
}

最新更新