2016-02-11 106 views

回答

0

为了处理HTTP请求,您可以使用RestClient库而不是编写所有低级请求。它节省了大量时间,并且不易出错。

例如,对于GET请求,所有你需要做的是:

String response = ""; 
int statusCode = client.post("/", "foo=bar", &response); 

One good such library与SSL支持由用户GitHub的书面DaKaz。

您可以将它用于您的GET请求。返回的响应将不包含HTTP头。该函数将返回没有标题的服务器的响应。

现在,您可以使用bblanchin的ArduinoJson库来解码JSON对象。

细节可以看出here.

或者你也可以做简单的字符串manipuation得到的数值虽然不是采取推荐的路线,而且容易出错。

0

这里是经由HTTP库发送JSON一个例子:

#include <ESP8266WiFi.h>  
    #include <ArduinoJson.h> 
    #include <ArduinoHttpClient.h> 

    #define JSON_BUF_SIZE  256 

    WiFiClient wifi; 
    HttpClient poster = HttpClient(wifi, IP, PORT); 

    void HTTPPost(){ 
     String contentType = "application/json"; 
     StaticJsonBuffer<JSON_BUF_SIZE> jsonBuffer; 
     JsonObject& jsonData = jsonBuffer.createObject(); 
     jsonData["valuename"] = "value"; 
     String postData = ""; 
     jsonData.printTo(postData); 
     poster.post("/", contentType, postData); 
     printf("Trace : ResponseCode : %d\n", poster.responseStatusCode()); 
     printf("Trace : Incoming Body : %s\n", poster.responseBody().c_str()); 
    }