Android: komunikacja HTTP powinna używać "Accept-Encoding: gzip"
Mam połączenie HTTP z serwerem żądającym danych JSON. Chciałbym skompresować ten strumień danych za pomocą Content-Encoding: gzip
. Czy istnieje sposób, w jaki mogę ustawić Accept-Encoding: gzip
w moim HttpClient? Wyszukiwanie gzip
w referencjach Androida nie pokazuje niczego związanego z HTTP, jak widać tutaj.
5 answers
Powinieneś użyć nagłówków http, aby wskazać, że połączenie może akceptować zakodowane dane gzip, np:
HttpUriRequest request = new HttpGet(url);
request.addHeader("Accept-Encoding", "gzip");
// ...
httpClient.execute(request);
Sprawdź odpowiedź na kodowanie treści:
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-16 06:53:18
Jeśli używasz API na poziomie 8 lub wyższym, istnieje AndroidHttpClient .
Posiada metody pomocnicze takie jak:
public static InputStream getUngzippedContent (HttpEntity entity)
I
public static void modifyRequestToAcceptGzipResponse (HttpRequest request)
Prowadzące do znacznie bardziej zwięzłego kodu:
AndroidHttpClient.modifyRequestToAcceptGzipResponse( request );
HttpResponse response = client.execute( request );
InputStream inputStream = AndroidHttpClient.getUngzippedContent( response.getEntity() );
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-08 18:10:53
Myślę, że próbka kodu pod tym linkiem jest ciekawsza: ClientGZipContentCompression.java
Używają HttpRequestInterceptor i HttpResponseInterceptor
Próbka dla zapytania:
httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
}
});
Próbka odpowiedzi:
httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(
new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-23 01:52:21
Nie używałem GZip, ale zakładam, że powinieneś używać strumienia wejściowego z HttpURLConnection
LUB HttpResponse
jako GZIPInputStream
, a nie jakiejś konkretnej innej klasy.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-15 18:44:52
W moim przypadku było tak:
URLConnection conn = ...;
InputStream instream = conn.getInputStream();
String encodingHeader = conn.getHeaderField("Content-Encoding");
if (encodingHeader != null && encodingHeader.toLowerCase().contains("gzip"))
{
instream = new GZIPInputStream(instream);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-27 08:39:47