2

我无法让我的代码设置目标温度(在 Android 中)。我在下面显示的代码接收到 OK 状态 (200) 以及当前温度作为有效载荷。我希望收到我写的新温度(65°F)。我正在使用网址:

https://developer-api.nest.com/devices/thermostats/THERMOSTAT-ID/target_temperature_f?auth=AUTH CODE

使用我的恒温器 ID 和验证码。我也有我的客户的读写权限。当我在 Chrome 扩展程序中执行类似的请求时,恒温器上的温度设置正确。谁能指出我在这里缺少什么?注释掉的代码显示了我尝试过的一些事情。

谢谢。

    InputStream is = null;
    String result = "";

    try{

        HttpClient httpclient = new DefaultHttpClient();
        HttpPut httpPut = new HttpPut(url);
        httpPut.addHeader("Content-Type", "application/json");
        httpPut.addHeader("Accept", "application/json");
        //httpPut.addHeader("Connection", "keep-alive");
        httpclient.getParams().setBooleanParameter("http.protocol.expect-continue",false);
        //value.setContentType("application/json");
        httpPut.setEntity(new StringEntity("65");
        HttpResponse response = httpclient.execute(httpPut);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        int statusCode = response.getStatusLine().getStatusCode();
        Log.d("StatusCode",Integer.toString(statusCode));

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
    }

    // Convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
        Log.d("Result",result);
    }catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
    }
4

4 回答 4

0

在某些条件下,您无法写入目标温度。有关规则,请参阅页面https://www.developer.nest.com/documentation/error-codes#targettemp

比如:在以下情况下,您无法保存目标温度:

  • 家处于“离开”模式。
  • 紧急加热正在使用中。
  • 暖通空调处于“关闭”模式。
  • 烟雾探测器触发了安全关闭。
于 2014-08-01T17:00:55.417 回答
0

该值必须作为 int (F) 和半浮点 (C) 传递。

于 2014-07-11T05:20:49.300 回答
0

这就是我改变温度的方法:

curl -v -L -X PUT "https://developer-api.nest.com/devices/thermostats/<THERMOSTAT ID>/target_temperature_f?auth=<AUTH CODE>" -H "Content-Type: application/json" -d "65"

你可以尝试修改你的标题看起来更像我跑的吗?我唯一的另一个猜测是 target_temperature_f 是一个整数,它没有作为 int 传递。

于 2014-07-10T19:31:48.733 回答
0

你能告诉我们你得到什么响应/代码吗?

更新:看起来您正在 Android 上使用它。我建议在 android 上使用 HttpsURLConnection。这是 HttpsURLConnection 的示例

    public static int setThermostatTemperatureF(int temperatureF, String apiUrl) {
      HttpsURLConnection.setFollowRedirects(true);
      BufferedReader reader = null;
      try {
        URL url = new URL(apiUrl);
        HttpsURLConnection ucon = (HttpsURLConnection) url.openConnection();
        ucon.setRequestProperty("Content-Type", "application/json");
        ucon.setRequestMethod("PUT");
        ucon.setDoOutput(true);
        // Write the PUT body
        OutputStreamWriter writer = new OutputStreamWriter(ucon.getOutputStream());
        writer.append(Integer.toString(temperatureF));
        writer.flush();

        return ucon.getResponseCode();
      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return -1;
    }

上一个答案:从代码看来,您可能没有遵循 developer-api.nest.com 返回的重定向。使用 HttpClient 4.2 及更高版本,您可以使用以下重定向策略来确保遵循临时重定向。

        httpClient.setRedirectStrategy(new DefaultRedirectStrategy() {
          @Override
          protected boolean isRedirectable(String method) {
            return HttpGet.METHOD_NAME.equals(method) ||
                    HttpPut.METHOD_NAME.equals(method);
          }
        });

如果您使用的是旧版本,则必须实现重定向处理程序。如果您需要该版本,请告诉我,我可以提供。

于 2014-07-11T06:18:23.177 回答