如何为HttpPut设置有效负载?我的代码不会写入目标温度

时间:2014-07-10 03:21:37

标签: nest-api

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

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 个答案:

答案 0 :(得分: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传递的。

答案 1 :(得分:0)

该值必须作为int(F)和half-float(C)传递。

答案 2 :(得分: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);
          }
        });

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

答案 3 :(得分:0)

在某些条件下,您无法写入目标温度。 查看页面https://www.developer.nest.com/documentation/error-codes#targettemp 对于规则。

喜欢:在以下情况下无法保存目标温度:

  • 房子离开&#39;模式。
  • 正在使用紧急加热。
  • hvac正在关闭&#39;模式。
  • 烟雾探测器绊倒了安全关闭装置。