GZIP Android запрашивает XML - PullRequest
       0

GZIP Android запрашивает XML

0 голосов
/ 18 августа 2011

У меня есть код, который я хотел бы преобразовать, чтобы я получал xml с сервера в формате GZIP. Я не уверен, как я буду отправлять свои запросы, чтобы увидеть, если кодировка принята, а что нет. Вот некоторый код:

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

Я прочитал другие посты и не уверен, где бы я реализовал предложения с этой кодировкой:

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

1 Ответ

0 голосов
/ 18 августа 2011

Ваш второй листинг кода заменит большую часть тела вашего getInputStream() метода в вашем первом листинге.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...