2011-08-21 16 views
6

参考:私は、しかし自動的に処理するのgzip HTTP応答

DefaultHttpClient httpclient = new DefaultHttpClient(); 
httpclient.addRequestInterceptor(new RequestAcceptEncoding()); 
httpclient.addResponseInterceptor(new ResponseContentEncoding()); 

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d4e1261

このページには、次のコードは、セットアップHttpClientが自動的に(HttpClientのユーザーに透明)のgzip応答を処理するだろうと言いますAndroid SDKにRequestAcceptEncodingResponseContentEncodingのクラスが見つかりません。彼らはちょうど行方不明ですか?私はこれらを自分で書く必要がありますか?ここで

答えて

11

は、私が使用するコードです:

mHttpClient.addResponseInterceptor(new HttpResponseInterceptor() { 
     public void process(final HttpResponse response, 
       final HttpContext context) throws HttpException, 
       IOException { 
      HttpEntity entity = response.getEntity(); 
      Header encheader = entity.getContentEncoding(); 
      if (encheader != null) { 
       HeaderElement[] codecs = encheader.getElements(); 
       for (int i = 0; i < codecs.length; i++) { 
        if (codecs[i].getName().equalsIgnoreCase("gzip")) { 
         response.setEntity(new GzipDecompressingEntity(
           entity)); 
         return; 
        } 
       } 
      } 
     } 
    }); 

あなたはまた、Google I/OアプリからSyncService.javaで見たいと思うかもしれません。

+0

正確APIレベルで導入された代わりにAndroidHttpClientを使用する - のおかげでも参考のためにSyncServiceへ –

+0

リンクが機能しません。それを是正してください。 –

+3

古いバージョンのApache HTTPクライアントを使用している場合は、 'GzipDecompressingEntitiy'が見つからないことがあります。あなたはそのコードをここで入手できます:http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/httpcore-contrib/src/main/java/org/apache/http/contrib/compress/GzipDecompressingEntity.java –

0

Androidは、欠けているクラスを持たないかなり古いバージョンのApache HTTP Clientライブラリにバンドルされています。

あなたはあなたのアプリケーションでのApache HTTPクライアントライブラリの新しいバージョンをバンドルすることができます(this答えを参照)、または私が必要なもの8.

+0

_このクラスはAPIレベル22では非推奨です。 – Prince

関連する問題