下面是androidstudio发送json数据的代码,应该是发送成功了,但是arduino显示"parseObject() failed" public void openLed(){ String url = "http://api.heclouds.com/cmds?device_id=xxxxxxxx"; RequestParams params = new RequestParams(url); params.addHeader("api-key","xxxxxxxxx"); params.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); params.addHeader("accept", "*/*"); params.addHeader("Connection", "Keep-Alive"); params.addHeader("Charset", "UTF-8"); params.addBodyParameter("code","led-0"); x.http().post(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { Log.e("bean",result); } @Override public void onError(Throwable ex, boolean isOnCallback) { Log.e("bean","error"); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { Log.e("bean","finish"); } }); } 下面是arduinoIDE解析json数据代码: void parseOneNetMqttResponse(char* payload){ Serial.println("start parseOneNetMqttResponse"); StaticJsonBuffer<300> jsonBuffer; // StaticJsonBuffer 在栈区分配内存 它也可以被 DynamicJsonBuffer(内存在堆区分配) 代替 // DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.parseObject(payload); // Test if parsing succeeds. if (!root.success()) { Serial.println("parseObject() failed"); return ; } String code = root["code"]; if(strcmp(code.c_str(),"code=led-1")==0) { i=1;digitalWrite(D7, LOW); } else if(strcmp(code.c_str(),"code=led-0")==1) { i=0;digitalWrite(D7, HIGH); } }
慕斯4360584
慕慕5436299
慕慕5436299