how to Get&Put xively data with android 2.3.6

636 Views Asked by At

Is this code will work in Android, I am using xively and xively's requirements are:

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

Please, could you help me to enable the gzip compression format

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

There are 1 best solutions below

0
On

Xively does not require using the gzip format, you can use it, but it is not required. Your problem here probably that you are not setting the API key in the header in your actual code. That should be the only header you need to specify. HttpClient should take care of the rest.