Code example for HttpURLConnection

Methods: getInputStreamgetURL

0
    } 
     
    public Feed handleFeed(HttpURLConnection connection) throws IOException, SAXException, ParserConfigurationException
    { 
        // connection.setRequestProperty("Accept-Encoding", "UTF-8"); 
        URL url = connection.getURL();
        InputStream stream = connection.getInputStream();
         
        byte[] bytes = IOUtils.toByteArray(stream);
         
        String encoding = getEncoding(connection, bytes);
         
        String s = new String(bytes, encoding);
         
        InputSource is = new InputSource(new StringReader(s));
         
        getParser().parse(is);
         
        // Reordering the list of items, first item parsed (most recent) -> last item in the list 
        Collections.reverse(mFeed.getItems());
        mFeed.setURL(url);
        if (mFeed.getHomePage() == null)
        { 
            mFeed.setHomePage(url);
        } 
         
        return mFeed;
    } 
     
    private String getEncoding(HttpURLConnection connection, byte[] bytes)
    { 
        String encoding = null;
        try 
        { 
            encoding = EncodingUtils.getEncoding(connection, bytes);
        } 
        catch (Exception e)
        { 
            Log.w(this.getClass().getName(), "Couldnot retrieve encoding", e); //$NON-NLS-1$
        }