Android Open Source - openaccessbutton Download Task






From Project

Back to project page openaccessbutton.

License

The source code is released under:

MIT License

If you think the Android project openaccessbutton 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

package org.openaccessbutton.openaccessbutton.blog;/*
 * Copyright (C) 2014 Open Access Button
 */*from   w w w  .  jav a  2  s .com*/
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

import android.os.AsyncTask;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Downloads (asynchronously) blog posts and calls a listener function with a List of parsed
 * Posts. Downloading XML based on the examples given in the Android documentation at
 * http://developer.android.com/training/basics/network-ops/xml.html.
 *
 * parameters: listener, page number
 */
public class DownloadTask extends AsyncTask<Object, Void, List<Post>> {
    /**
     * Called with the List of Posts when the download is finished
     */
    public interface OnDownloadCompleteListener {
        void onDownloadComplete(List<Post> posts);
    }
    OnDownloadCompleteListener mListener;

    /**
     * Blog feed URL
     */
    public static String BLOG_URL = "http://blog.openaccessbutton.org/feed/";

    @Override
    protected List<Post> doInBackground(Object... params) {
        // Extract the parameters
        mListener = (OnDownloadCompleteListener) params[0];
        int pageNumber = (Integer) params[1];

        // Create url
        // e.g., /feed/?paged=2
        String url = BLOG_URL + "?paged=" + Integer.toString(pageNumber);

        try {
            return loadPostsFromNetwork(url);
        } catch (IOException e) {
            // TODO: Show UI messages for these errors
            return new ArrayList<Post>();
        } catch (XmlPullParserException e) {
            return new ArrayList<Post>();
        }
    }

    @Override
    protected void onPostExecute(List<Post> posts) {
        // Call the listener
        mListener.onDownloadComplete(posts);
    }

    private List<Post> loadPostsFromNetwork(String urlString) throws XmlPullParserException,
            IOException {
        InputStream stream = null;
        RssParser rssParser = new RssParser();
        List<Post> posts = null;

        // Download and parse
        try {
            stream = downloadUrl(urlString);
            posts = rssParser.parse(stream);
        // Make sure input stream is closed
        } finally {
            if (stream != null) {
                stream.close();
            }
        }

        return posts;
    }

    private InputStream downloadUrl(String urlString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect();

        return conn.getInputStream();
    }
}




Java Source Code List

com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java
org.openaccessbutton.openaccessbutton.ApplicationTest.java
org.openaccessbutton.openaccessbutton.MainActivity.java
org.openaccessbutton.openaccessbutton.NavigationItem.java
org.openaccessbutton.openaccessbutton.NavigationXmlParser.java
org.openaccessbutton.openaccessbutton.OnFragmentNeededListener.java
org.openaccessbutton.openaccessbutton.OnShareIntentInterface.java
org.openaccessbutton.openaccessbutton.about.AboutActivity.java
org.openaccessbutton.openaccessbutton.advocacy.AdvocacyFragment.java
org.openaccessbutton.openaccessbutton.advocacy.FaqView.java
org.openaccessbutton.openaccessbutton.advocacy.Question.java
org.openaccessbutton.openaccessbutton.advocacy.QuestionsActivity.java
org.openaccessbutton.openaccessbutton.advocacy.XmlParser.java
org.openaccessbutton.openaccessbutton.api.API.java
org.openaccessbutton.openaccessbutton.blog.BlogAdapter.java
org.openaccessbutton.openaccessbutton.blog.BlogDetailsFragment.java
org.openaccessbutton.openaccessbutton.blog.BlogFragment.java
org.openaccessbutton.openaccessbutton.blog.DownloadTask.java
org.openaccessbutton.openaccessbutton.blog.Post.java
org.openaccessbutton.openaccessbutton.blog.RssParser.java
org.openaccessbutton.openaccessbutton.browser.BrowserFragment.java
org.openaccessbutton.openaccessbutton.browser.ScrollingWebView.java
org.openaccessbutton.openaccessbutton.button.ButtonSubmitActivity.java
org.openaccessbutton.openaccessbutton.intro.ForgotPasswordActivity.java
org.openaccessbutton.openaccessbutton.intro.IntroActivity.java
org.openaccessbutton.openaccessbutton.intro.LaunchActivity.java
org.openaccessbutton.openaccessbutton.intro.ScreenSlidePageFragment.java
org.openaccessbutton.openaccessbutton.intro.SigninActivity.java
org.openaccessbutton.openaccessbutton.intro.SigninButtonClickListener.java
org.openaccessbutton.openaccessbutton.intro.SignupActivity.java
org.openaccessbutton.openaccessbutton.intro.SignupEmailButtonClickListener.java
org.openaccessbutton.openaccessbutton.map.Item.java
org.openaccessbutton.openaccessbutton.map.MapFragment.java
org.openaccessbutton.openaccessbutton.menu.MenuActivity.java
org.openaccessbutton.openaccessbutton.preferences.AppPreferencesActivity.java
org.openaccessbutton.openaccessbutton.push.PushReceiver.java
org.openaccessbutton.openaccessbutton.push.Push.java