【发布时间】:2020-12-11 13:07:31
【问题描述】:
我想使用飞利浦 HUE Api 通过 Java 关灯。 为此,我有以下 API:http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/
这会返回:
{
"state": {
"on": true,
"bri": 178,
"hue": 41044,
"sat": 56,
"effect": "none",
"xy": [
0.3313,
0.3447
],
"ct": 181,
"alert": "select",
"colormode": "ct",
"mode": "homeautomation",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": "2020-07-03T12:17:24"
},
"type": "Extended color light",
"name": "Hue color spot 1",
"modelid": "LCG002",
"manufacturername": "Signify Netherlands B.V.",
"swconfigid": "9FD98F72"
}
现在我想用 Java 将“on:true”修改为“on:false”。为此,我有这个代码:
URL url = new URL("http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(60000);
connection.setReadTimeout(60000);
connection.setRequestProperty("Content-Type", "application/json");
JSONObject requestBody = new JSONObject();
requestBody.put("on", false);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(requestBody.toString());
writer.flush();
writer.close();
// Debug
int response = connection.getResponseCode();
System.out.println(requestBody.toString());
System.out.println(response);
这将返回 '200' 和 {"on":false} ,这将是完美的。 但由于某种原因,它什么也没做。即使存在连接,灯仍然亮着。
有什么想法吗?
【问题讨论】:
-
“它不发送”到底是什么意思?
-
当您尝试通过命令行卷曲它时,无论是 on:true 还是 on:false,响应代码是什么?例如: curl -X PUT -d '{"on":true}' 192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/…
-
我在 Windows Shell 和 git bash (Linux) 中都执行了命令
curl -X PUT -d '{"on":false}' 192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/state。它在 git bash 中有效,但在 cmd 中无效。 -
@tgdavies 我编辑并试图更好地解释它。
-
您需要有另一个api(链接)进行编辑。
标签: java http http-put philips-hue