Android Open Source - SimpleReader R S S Reader






From Project

Back to project page SimpleReader.

License

The source code is released under:

Apache License

If you think the Android project SimpleReader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2010 A. Horn/*from www  .  j  ava 2 s  .c o m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dreamteam.app.rss;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * HTTP client to retrieve and parse RSS 2.0 feeds. Callers must call
 * {@link RSSReader#close()} to release all resources.
 * 
 * @author Mr Horn
 */
public class RSSReader implements java.io.Closeable {

  /**
   * Thread-safe {@link HttpClient} implementation.
   */
  private final HttpClient httpclient;

  /**
   * Thread-safe RSS parser SPI.
   */
  private final RSSParserSPI parser;

  /**
   * Instantiate a thread-safe HTTP client to retrieve RSS feeds. The injected
   * {@link HttpClient} implementation must be thread-safe.
   * 
   * @param httpclient thread-safe HTTP client implementation
   * @param parser thread-safe RSS parser SPI implementation
   */
  public RSSReader(HttpClient httpclient, RSSParserSPI parser) {
    this.httpclient = httpclient;
    this.parser = parser;
  }

  /**
   * Instantiate a thread-safe HTTP client to retrieve RSS feeds. The injected
   * {@link HttpClient} implementation must be thread-safe. Internal memory
   * consumption and load performance can be tweaked with {@link RSSConfig}.
   * 
   * @param httpclient thread-safe HTTP client implementation
   * @param config RSS configuration
   */
  public RSSReader(HttpClient httpclient, RSSConfig config) {
    this(httpclient, new RSSParser(config));
  }

  /**
   * Instantiate a thread-safe HTTP client to retrieve and parse RSS feeds.
   * Internal memory consumption and load performance can be tweaked with
   * {@link RSSConfig}.
   */
  public RSSReader(RSSConfig config) {
    this(new DefaultHttpClient(), new RSSParser(config));
  }

  /**
   * Instantiate a thread-safe HTTP client to retrieve and parse RSS feeds.
   * Default RSS configuration capacity values are used.
   */
  public RSSReader() {
    this(new DefaultHttpClient(), new RSSParser(new RSSConfig()));
  }

  /**
   * Send HTTP GET request and parse the XML response to construct an in-memory
   * representation of an RSS 2.0 feed.
   * 
   * @param uri RSS 2.0 feed URI
   * @return in-memory representation of downloaded RSS feed
   * @throws RSSReaderException if RSS feed could not be retrieved because of
   *           HTTP error
   * @throws RSSFault if an unrecoverable IO error has occurred
   */
  public RSSFeed load(String uri) throws RSSReaderException {
    final HttpGet httpget = new HttpGet(uri);

    InputStream feedStream = null;
    try {
      // Send GET request to URI
      final HttpResponse response = httpclient.execute(httpget);

      // Check if server response is valid
      final StatusLine status = response.getStatusLine();
      if (status.getStatusCode() != HttpStatus.SC_OK) {
        throw new RSSReaderException(status.getStatusCode(),
            status.getReasonPhrase());
      }

      // Extract content stream from HTTP response
      HttpEntity entity = response.getEntity();
      feedStream = entity.getContent();

      RSSFeed feed = parser.parse(feedStream);

      if (feed.getLink() == null) {
        feed.setLink(android.net.Uri.parse(uri));
      }

      return feed;
    } catch (ClientProtocolException e) {
      throw new RSSFault(e);
    } catch (IOException e) {
      throw new RSSFault(e);
    } finally {
      Resources.closeQuietly(feedStream);
    }
  }

  /**
   * Release all HTTP client resources.
   */
  public void close() {
    httpclient.getConnectionManager().shutdown();
  }

}




Java Source Code List

com.dreamteam.app.adapter.CategoryDetailAdapter.java
com.dreamteam.app.adapter.FeedCategoryAdapter.java
com.dreamteam.app.adapter.GridAdapter.java
com.dreamteam.app.adapter.GuideViewPagerAdapter.java
com.dreamteam.app.adapter.ItemListAdapter.java
com.dreamteam.app.adapter.MPagerAdapter.java
com.dreamteam.app.commons.AppConfig.java
com.dreamteam.app.commons.AppContext.java
com.dreamteam.app.commons.HtmlFilter.java
com.dreamteam.app.commons.IFlyHelper.java
com.dreamteam.app.commons.ItemListEntityParser.java
com.dreamteam.app.commons.SectionHelper.java
com.dreamteam.app.commons.SeriaHelper.java
com.dreamteam.app.commons.SkinManager.java
com.dreamteam.app.commons.UIHelper.java
com.dreamteam.app.config.Contants.java
com.dreamteam.app.db.DbManager.java
com.dreamteam.app.db.FavoItemDbHelper.java
com.dreamteam.app.db.FeedDBManager.java
com.dreamteam.app.db.provider.RSSFeedCategoryProvider.java
com.dreamteam.app.entity.FeedItem.java
com.dreamteam.app.entity.Feed.java
com.dreamteam.app.entity.ItemListEntity.java
com.dreamteam.app.entity.RSSFeedCategroy.java
com.dreamteam.app.entity.Section.java
com.dreamteam.app.img.FileCacheManager.java
com.dreamteam.app.img.FileCache.java
com.dreamteam.app.img.ICache.java
com.dreamteam.app.img.ImageLoadTask.java
com.dreamteam.app.img.ImageLoad.java
com.dreamteam.app.img.ImageLoader.java
com.dreamteam.app.img.MemoryCache.java
com.dreamteam.app.rss.Dates.java
com.dreamteam.app.rss.Integers.java
com.dreamteam.app.rss.MediaAttributes.java
com.dreamteam.app.rss.MediaEnclosure.java
com.dreamteam.app.rss.MediaThumbnail.java
com.dreamteam.app.rss.RSSBase.java
com.dreamteam.app.rss.RSSConfig.java
com.dreamteam.app.rss.RSSException.java
com.dreamteam.app.rss.RSSFault.java
com.dreamteam.app.rss.RSSFeed.java
com.dreamteam.app.rss.RSSHandler.java
com.dreamteam.app.rss.RSSItem.java
com.dreamteam.app.rss.RSSLoader.java
com.dreamteam.app.rss.RSSParserSPI.java
com.dreamteam.app.rss.RSSParser.java
com.dreamteam.app.rss.RSSReaderException.java
com.dreamteam.app.rss.RSSReader.java
com.dreamteam.app.rss.Resources.java
com.dreamteam.app.ui.About.java
com.dreamteam.app.ui.BaseActivity.java
com.dreamteam.app.ui.BaseTitledActivity.java
com.dreamteam.app.ui.CategoryDetailActivity.java
com.dreamteam.app.ui.ColorListActivity.java
com.dreamteam.app.ui.FavoriteItemList.java
com.dreamteam.app.ui.FeedCategoryActivity.java
com.dreamteam.app.ui.FeedbackUI.java
com.dreamteam.app.ui.GuideActivity.java
com.dreamteam.app.ui.ImageDialog.java
com.dreamteam.app.ui.ImagesBrowseActivity.java
com.dreamteam.app.ui.ItemDetailActivity.java
com.dreamteam.app.ui.ItemListActivity.java
com.dreamteam.app.ui.LocalImageBrowseActivity.java
com.dreamteam.app.ui.LoginDialog.java
com.dreamteam.app.ui.MainActivity.java
com.dreamteam.app.ui.Setting.java
com.dreamteam.app.ui.SplashActivity.java
com.dreamteam.app.ui.SwitchBgActivity.java
com.dreamteam.app.ui.WidgetProvider.java
com.dreamteam.app.ui.adapter.ColorListAdapter.java
com.dreamteam.app.utils.CategoryNameExchange.java
com.dreamteam.app.utils.DateUtils.java
com.dreamteam.app.utils.FileUtils.java
com.dreamteam.app.utils.HttpUtils.java
com.dreamteam.app.utils.ImageLoader.java
com.dreamteam.app.utils.ImageUtils.java
com.dreamteam.app.utils.Logger.java
com.dreamteam.app.utils.MD5.java
com.dreamteam.app.utils.StringUtils.java
com.dreamteam.app.wallpaper.ChildAdapter.java
com.dreamteam.app.wallpaper.GroupGridAdapter.java
com.dreamteam.app.wallpaper.ImageBean.java
com.dreamteam.app.wallpaper.MyImageView.java
com.dreamteam.app.wallpaper.NativeImageLoader.java
com.dreamteam.app.wallpaper.WallPaperManager.java
com.dreamteam.custom.ui.PathAnimations.java
com.dreamteam.custom.ui.PullToRefreshListView.java