Android GZIP请求XML

Android GZIP请求XML,android,gzip,Android,Gzip,我有我想要转换的代码,以便以GZIP格式从服务器获取xml。我不确定我将如何发送我的请求,以查看编码是否被接受,以及什么不被接受。下面是一些代码: public void parse(String locCode, int isMetric, String langId) throws IOException, ParserConfigurationException, SAXException { //set member variables this.locCode

我有我想要转换的代码,以便以GZIP格式从服务器获取xml。我不确定我将如何发送我的请求,以查看编码是否被接受,以及什么不被接受。下面是一些代码:

    public void parse(String locCode, int isMetric, String langId) throws IOException, ParserConfigurationException, SAXException {
    //set member variables
    this.locCode    = locCode;
    this.isMetric   = isMetric;
    this.langId     = langId;
    wdm.metric      = isMetric;

    try {
        mCounter++;
        //create input stream from url
        InputStream is = getInputStream(this.locCode, this.isMetric, this.langId);  
        InputSource inputSource = new InputSource(is);

        inputSource.setEncoding(ENCODING_TYPE);

        //create sax parser and xml reader
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        mCounter = 0;
        //pass in instance of FeedParser
        xr.setContentHandler(new WeatherFeedParser());
        xr.parse(inputSource);
        is.close();
    } catch (SocketException e){
        if (mCounter < 3){
            parse(locCode, isMetric, langId);
        }
        else e.printStackTrace();
    } catch (UnknownHostException e){
        if (mCounter < 3){
            parse(locCode, isMetric, langId);
        }
        else e.printStackTrace();
    }

}

private static final InputStream getInputStream(String locCode, int isMetric, String langId) throws IOException {
    //build url
    String addr = FEED_URL + LOCATION + cleanupInput(locCode) + "&" + METRIC + isMetric + "&" + LANG_ID + langId;
    URL url = new URL(addr);

    //create connection
    mCon = url.openConnection();
    mCon.setConnectTimeout((int)ACCUWX.Time._15_SECONDS);
    mCon.setReadTimeout((int)ACCUWX.Time._15_SECONDS);
    mCon.connect();

    return mCon.getInputStream();        
}

第二个代码清单将替换第一个清单中的
getInputStream()
方法的大部分内容。

使用
gzip解压缩实体查看第二个代码示例的替代实现。
HttpUriRequest request = new HttpGet(url);
request.addHeader("Accept-Encoding", "gzip");
// ...
httpClient.execute(request);
Check response for content encoding:
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
    instream = new GZIPInputStream(instream);
}