[英]HttpGet in Android/Java with GZip encoding
我在服務器上運行MVC 4,並為用戶節省了一些數據,我認為我將啟用GZip編碼,為此,我只使用了以下方法:
(C#)
Response.AddHeader("Content-Encoding", "gzip");
Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress);
在我的android應用程序中,我使用:
(Java)
String response = "";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
當我使用GZip結束Java代碼並導致GC運行時,我從來沒有足夠的耐心等待它返回。
當我從服務器上取下GZip時,它運行得非常好。 獲得響應的函數將立即返回,沒有問題。
我嘗試將其添加到Java代碼中:
httpGet.addHeader("Accept-Encoding", "gzip");
沒有成功。
問題是,我沒有得到什么? 如果正在使用GZip,我是否不能將響應放入流中? 我是要使用流並在之后解壓縮嗎?
我究竟做錯了什么?
而不是使用
DefaultHttpClient client = new DefaultHttpClient();
您可以使用
ContentEncodingHttpClient client = new ContentEncodingHttpClient();
這是DefaultHttpClient
的子類,並支持GZIP內容。 為此,您需要Apache HttpClient 4.1。
如果您有Apache HttpClient 4.2,則應使用
DecompressingHttpClient client = new DecompressingHttpClient();
如果您有Apache HttpClient 4.3,則應使用HttpClientBuilder
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.