2011年8月19日金曜日

android HTTP通信でgzipファイルをゲット


// HTTP通信、パラメータ渡して、GZipファイルを返してくる

public String doPost() {

StringBuffer sb = new StringBuffer();

String url = "http://google.map";
// String lat = "35.000002";
// String lon = "139.0000003";

String lat = keido;
String lon = ido;

try {
HttpPost request = new HttpPost(url);

List<NameValuePair> params = new LinkedList<NameValuePair>();
params.add(new BasicNameValuePair("lon", lon));
params.add(new BasicNameValuePair("lat", lat));

request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
request.setHeader("Accept-Encoding", "gzip");

HttpResponse response = new DefaultHttpClient().execute(request);

int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
HttpEntity httpEntity = response.getEntity();

InputStream is = httpEntity.getContent();
if (httpEntity.getContentEncoding().getValue().contains("gzip")) {
is = new GZIPInputStream(is);
InputStreamReader in = new InputStreamReader(is);
BufferedReader br = new BufferedReader(in);

String line;
while ((line = br.readLine()) != null) {
sb.append(line);

}
br.close();
in.close();
is.close();

}
}

} catch (Exception e) {

System.out.println("dopost------" + e);
}
return sb.toString();
}

0 件のコメント: