如何使用android 2.3.6获取和放置xively数据

时间:2013-12-30 08:25:10

标签: android xively

此代码是否适用于Android,我使用xively和xively的要求是:

X-ApiKey            API_KEY_HERE
User-Agent          Device Agent
Content-Length      length
Host                api.xively.com
Content-Encoding    utf-8,gzip

请帮助我启用gzip压缩格式

public String httpGet(String s) {
String url = s;
StringBuilder body = new StringBuilder();
httpclient = new DefaultHttpClient(); // create new httpClient
HttpGet httpGet = new HttpGet(url); // create new httpGet object

try {
    response = httpclient.execute(httpGet); // execute httpGet
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == HttpStatus.SC_OK) {
        // System.out.println(statusLine);
        body.append(statusLine + "\n");
        HttpEntity e = response.getEntity();
        String entity = EntityUtils.toString(e);
        body.append(entity);
    } else {
        body.append(statusLine + "\n");
        // System.out.println(statusLine);
    }
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    httpGet.releaseConnection(); // stop connection
}
return body.toString(); // return the String
}

1 个答案:

答案 0 :(得分:0)

Xively不需要使用gzip格式,您可以使用它,但不是必需的。您的问题可能是您没有在实际代码的标题中设置API密钥。这应该是您需要指定的唯一标头。 HttpClient应该处理剩下的事情。