我无法让我的代码设置目标温度(在 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());
}