109

JSONデータを要求するWebサーバーへのHTTP通信があります。このデータストリームをで圧縮したいのですがContent-Encoding: gzipAccept-Encoding: gzipHttpClientで設定する方法はありますか?Androidリファレンスでの検索では、ここgzipに表示されているように、HTTPに関連するものは何も表示されませ

4

5 に答える 5

174

httpヘッダーを使用して、接続がgzipでエンコードされたデータを受け入れることができることを示す必要があります。例:

HttpUriRequest request = new HttpGet(url);
request.addHeader("Accept-Encoding", "gzip");
// ...
httpClient.execute(request);

コンテンツエンコーディングの応答を確認します。

InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
    instream = new GZIPInputStream(instream);
}
于 2009-10-16T06:53:18.183 に答える
33

APIレベル8以上を使用している場合は、AndroidHttpClientがあります。

次のようなヘルパーメソッドがあります。

public static InputStream getUngzippedContent (HttpEntity entity)

public static void modifyRequestToAcceptGzipResponse (HttpRequest request)

はるかに簡潔なコードにつながる:

AndroidHttpClient.modifyRequestToAcceptGzipResponse( request );
HttpResponse response = client.execute( request );
InputStream inputStream = AndroidHttpClient.getUngzippedContent( response.getEntity() );
于 2012-02-08T18:10:53.080 に答える
13

このリンクのコードのサンプルはもっと面白いと思います: ClientGZipContentCompression.java

彼らはHttpRequestInterceptorHttpResponseInterceptorを使用しています

リクエストのサンプル:

        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");
                }
            }

        });

回答のサンプル:

        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;
                        }
                    }
                }
            }

        });
于 2011-07-23T01:52:21.210 に答える
1

私はGZipを使用していませんが、他の特定のクラスではなく、HttpURLConnectionまたはHttpResponseとしての入力ストリームを使用する必要があると思います。GZIPInputStream

于 2009-10-15T18:44:52.643 に答える
0

私の場合は次のようになりました。

URLConnection conn = ...;
InputStream instream = conn.getInputStream();
String encodingHeader = conn.getHeaderField("Content-Encoding");
if (encodingHeader != null && encodingHeader.toLowerCase().contains("gzip"))
{
    instream = new GZIPInputStream(instream);
}
于 2014-03-27T08:39:47.130 に答える