1

这段代码是否可以在 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
}
4

1 回答 1

0

Xively不需要使用gzip格式,可以使用,但不是必须的。您的问题可能是您没有在实际代码的标头中设置 API 密钥。这应该是您需要指定的唯一标题。HttpClient 应该负责其余的工作。

于 2013-12-31T17:01:55.667 回答