2010-12-07 60 views
3

我只是想問一下在Android中使用HttpClient發送gzip以發送郵件請求?Android中的GZIP

從哪裏得到OutputStream在GZIPOutputstream中傳遞?

任何代碼段?

+0

看看這個鏈接http://stackoverflow.com/q/6717165/779408 – breceivemail 2013-01-15 10:38:14

回答

7

嗨UseHttpUriRequest如下圖所示

String urlval=" http"//www.sampleurl.com/"; 
    HttpUriRequest req = new HttpGet(urlval); 
    req.addHeader("Accept-Encoding", "gzip"); 
    httpClient.execute(req); 

,然後檢查內容編碼響應,如下圖所示:

InputStream is = response.getEntity().getContent(); 
Header contentEncoding = response.getFirstHeader("Content-Encoding"); 
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { 
    is = new GZIPInputStream(is); 
} 
+1

這個人問有關發送gzip' d請求。上面告訴服務器你可以接受gzip'd響應,並在你得到它們時處理它們,但它不會gzip傳出的內容。 – 2011-05-04 17:53:09

2

如果你的數據不是太大,你可以做這樣的:

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpost = new HttpPost(POST_URL); 

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
GZIPOutputStream gos = new GZIPOutputStream(baos); 
gos.write(data.getBytes()); 
gos.close(); 
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(baos.toByteArray()); 
httpost.setEntity(byteArrayEntity);