Android Open Source - TorrentFreak-Reader Article Content Http Task






From Project

Back to project page TorrentFreak-Reader.

License

The source code is released under:

GNU General Public License

If you think the Android project TorrentFreak-Reader 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) 2013 Jack Wakefield/*from w ww.  j a v  a2  s  . c  om*/
 *
 * This file is part of TorrentFreak Reader.
 *
 * TorrentFreak Reader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TorrentFreak Reader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TorrentFreak Reader.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.torrentfreak.reader.free.articles.tasks;

import java.io.IOException;
import java.lang.Exception;
import java.lang.ref.WeakReference;
import java.util.Map;
import android.os.AsyncTask;
import com.torrentfreak.reader.free.articles.providers.ArticleContentProvider;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Cleaner;
import org.jsoup.safety.Whitelist;

public class ArticleContentHttpTask extends AsyncTask<String, Void, Map<String, String>> {
    /**
     * The article content provider.
     */
    private final ArticleContentProvider provider;

    /**
     * The weak reference to the article received listener.
     */
    private final WeakReference<ArticleContentProvider.OnArticleReceivedListener> listenerReference;

    /**
     * The exception which occurred.
     */
    private Exception error;

    public ArticleContentHttpTask(final ArticleContentProvider provider,
        final WeakReference<ArticleContentProvider.OnArticleReceivedListener> listenerReference) {
        this.provider = provider;
        this.listenerReference = listenerReference;
    }

    @Override
    public Map<String, String> doInBackground(final String... params) {
        Document document;

        try {
            // retrieve the document
            document = Jsoup.connect(params[0]).timeout(20000).ignoreHttpErrors(true).
                followRedirects(true).get();
        } catch (IOException e) {
            error = e;
            return null;
        }

        // setup the whitelist of elements and attributes to allow
        final Whitelist whitelist = Whitelist.relaxed();
        whitelist.addTags("abbr", "address", "area", "article", "aside", "embed", "footer",
            "header", "hr", "iframe", "label", "legend", "nav", "object", "param", "s", "section",
            "summary", "time", "video", "track", "wbr", "center");
        whitelist.addAttributes("a", "rel");
        whitelist.addAttributes("ul", "id");
        whitelist.addAttributes("li", "class");
        whitelist.addAttributes("img", "class");
        whitelist.addAttributes("img", "align");
        whitelist.addAttributes("span", "class");
        whitelist.addAttributes("table", "class");
        whitelist.addAttributes("p", "class");
        whitelist.addAttributes("iframe", "src", "scrolling", "width", "height", "frameborder");

        // clear the retrieved document with the whitelist
        final Cleaner cleaner = new Cleaner(whitelist);
        document = cleaner.clean(document);

        Map<String, String> values = null;

        try {
            // scrape the required values from the document using the article provider
            values = provider.scrape(document);
        } catch (Exception e) {
            error = e;
            return null;
        }

        return values;
    }

    @Override
    public void onPostExecute(final Map<String, String> values) {
        // retrieve the listener from the weak reference
        final ArticleContentProvider.OnArticleReceivedListener listener = listenerReference.get();

        // ensure the task hasn't been cancelled and that the listener still exists
        if (!isCancelled() && listener != null) {
            // determine whether any values were set
            if (values != null) {
                // inform the listener the article was received
                listener.onArticleReceived(values);
            }

            // determine whether an exception was set
            if (error != null) {
                // inform the listener an error occurred
                listener.onArticleReceivedError(error);
            }
        }
    }
}




Java Source Code List

com.slidingmenu.lib.CanvasTransformerBuilder.java
com.slidingmenu.lib.CustomViewAbove.java
com.slidingmenu.lib.CustomViewBehind.java
com.slidingmenu.lib.MenuInterface.java
com.slidingmenu.lib.SlidingMenu.java
com.slidingmenu.lib.app.SlidingActivityBase.java
com.slidingmenu.lib.app.SlidingActivityHelper.java
com.slidingmenu.lib.app.SlidingActivity.java
com.slidingmenu.lib.app.SlidingFragmentActivity.java
com.slidingmenu.lib.app.SlidingListActivity.java
com.slidingmenu.lib.app.SlidingPreferenceActivity.java
com.torrentfreak.reader.free.AboutActivity.java
com.torrentfreak.reader.free.ArticleActivity.java
com.torrentfreak.reader.free.LicensesActivity.java
com.torrentfreak.reader.free.MainActivity.java
com.torrentfreak.reader.free.SettingsActivity.java
com.torrentfreak.reader.free.adapters.ArticleFragmentAdapter.java
com.torrentfreak.reader.free.adapters.ArticleListAdapter.java
com.torrentfreak.reader.free.adapters.LicenseFragmentAdapter.java
com.torrentfreak.reader.free.adapters.SlidingMenuAdapter.java
com.torrentfreak.reader.free.adapters.items.LicenseItem.java
com.torrentfreak.reader.free.adapters.items.SlidingMenuItem.java
com.torrentfreak.reader.free.adapters.views.ArticleItemView.java
com.torrentfreak.reader.free.adapters.views.SlidingMenuItemView.java
com.torrentfreak.reader.free.articles.ArticleItem.java
com.torrentfreak.reader.free.articles.ArticleStorage.java
com.torrentfreak.reader.free.articles.providers.ArticleContentProvider.java
com.torrentfreak.reader.free.articles.providers.ArticleListProvider.java
com.torrentfreak.reader.free.articles.providers.CategoryListProvider.java
com.torrentfreak.reader.free.articles.providers.LatestNewsListProvider.java
com.torrentfreak.reader.free.articles.providers.exceptions.ArticleScrapeException.java
com.torrentfreak.reader.free.articles.tasks.ArticleContentHttpTask.java
com.torrentfreak.reader.free.categories.CategoryItem.java
com.torrentfreak.reader.free.categories.CategoryManager.java
com.torrentfreak.reader.free.categories.CategoryType.java
com.torrentfreak.reader.free.fragments.ArticleCommentsFragment.java
com.torrentfreak.reader.free.fragments.ArticleContentFragment.java
com.torrentfreak.reader.free.fragments.ArticleListFragment.java
com.torrentfreak.reader.free.fragments.LicenseFragment.java
com.torrentfreak.reader.free.fragments.SlidingMenuFragment.java
com.torrentfreak.reader.free.helpers.FontHelper.java
com.torrentfreak.reader.free.helpers.WeakReferenceHelper.java
com.torrentfreak.reader.free.widgets.StackWidgetActivity.java
com.torrentfreak.reader.free.widgets.StackWidgetProvider.java
com.torrentfreak.reader.free.widgets.StackWidgetService.java
com.torrentfreak.reader.free.widgets.items.StackWidgetItem.java