2017-05-13 204 views
0

我一直试图在我的ESP8266上获取天气信息以在OLED显示屏上显示它们。ESP8266 Arduino IDE JSON HTTP请求

if (!client.connect("api.apixu.com", 80)) { 
    Serial.println("connection failed"); 
    return;} 

    client.print("GET " + url + " HTTP/1.1\r\n" + 
    "Host: " + host + "\r\n" + 
    "Content-Type: application/json\r\n" + 
    "Connection: close\r\n\r\n"); 

    char c[1024]; 
    // Read all the lines of the reply from server and print them to Serial 
    while(client.available()){ 
     c[0] = client.read();Serial.print(c);} 

这是我的代码,用于请求和阅读JSON内容。但无论我尝试,代码都不会进入while循环。

有人能发现错误吗?

预先感谢您! :d

回答

0

我固定它在某种程度上此代码为大家谁了相同的错误available()总是0

client.print(String("GET ") + url + " HTTP/1.1\r\n" + 
       "Host: " + host + "\r\n" + 
       "Connection: close\r\n\r\n"); 
    unsigned long timeout = millis(); 
    while (client.available() == 0) { 
    if (millis() - timeout > 5000) { 
     Serial.println(">>> Client Timeout !"); 
     client.stop(); 
     return; 
    }