com.lillicoder.newsblurry.stories.DownloadStoriesTask.java Source code

Java tutorial

Introduction

Here is the source code for com.lillicoder.newsblurry.stories.DownloadStoriesTask.java

Source

/**
 * Copyright 2012 Scott Weeden-Moody
 *
 * 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.lillicoder.newsblurry.stories;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.lillicoder.newsblurry.R;
import com.lillicoder.newsblurry.exception.ApiRequestException;
import com.lillicoder.newsblurry.net.BaseTask;
import com.lillicoder.newsblurry.net.OnTaskCompletedListener;

/**
 * Task that downloads the stories for a given feed.
 * @author lillicoder
 */
public class DownloadStoriesTask extends BaseTask<Integer, Void, List<Story>> {

    private static final String TAG = DownloadStoriesTask.class.getSimpleName();

    protected DownloadStoriesTask(Context context, OnTaskCompletedListener<List<Story>> listener) {
        super(context, listener);
    }

    @Override
    protected List<Story> doInBackground(Integer... params) {
        try {
            Context context = this.getContext();

            int feedId = params[0];
            String storiesUrl = String.format(context.getString(R.string.api_storiesUrl), feedId);
            HttpGet storiesGet = new HttpGet(storiesUrl);

            // TODO Add name-value pair parameter for page number
            // int pageNumber = params[1];

            // Pull entity and parse JSON response.
            HttpResponse httpResponse = this.executeWebRequest(storiesGet);
            JSONObject responseJson = this.getJsonFromResponse(httpResponse);

            StoryParser parser = new StoryParser();
            return parser.parseStories(responseJson);
        } catch (IndexOutOfBoundsException e) {
            Log.e(TAG, EXCEPTION_FAILED_TO_PARAMS);
            this.setException(e);
        } catch (ApiRequestException e) {
            Log.e(TAG, EXCEPTION_API_REQUEST_FAILED, e);
            this.setException(e);
        } catch (IOException e) {
            Log.e(TAG, EXCEPTION_FAILED_TO_PARSE_RESPONSE, e);
            this.setException(e);
        } catch (JSONException e) {
            Log.e(TAG, EXCEPTION_UNABLE_TO_CREATE_JSON_FROM_RESPONSE, e);
            this.setException(e);
        }

        return null;
    }

}