Android Open Source - Android-iTunes-Client-Library Podcast Entries Async Task






From Project

Back to project page Android-iTunes-Client-Library.

License

The source code is released under:

Apache License

If you think the Android project Android-iTunes-Client-Library 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 com.tassioauad.androiditunesclientlibrary.service;
/* ww w  . j  a v  a  2 s. c o  m*/
import android.os.AsyncTask;

import com.tassioauad.androiditunesclientlibrary.core.OnApiResultListener;
import com.tassioauad.androiditunesclientlibrary.service.entity.PodcastEntryEntity;
import com.google.gson.Gson;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Tssio on 09/06/2014.
 */
public class PodcastEntriesAsyncTask extends AsyncTask<String, Void, PodcastEntryEntity[]> {

    private OnApiResultListener listener;

    @Override
    protected PodcastEntryEntity[] doInBackground(String... strings) {

        String feedUrl = strings[0];
        try {

            OkHttpClient client = new OkHttpClient();

            Request request = new Request.Builder()
                    .url("http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=100&q=" + feedUrl)
                    .build();

            Response response = client.newCall(request).execute();

            String jsonResponsed = getJson((InputStreamReader) response.body().charStream());
            return new Gson().fromJson(jsonResponsed, PodcastEntryEntity[].class);

        } catch (Exception e) {
            return null;
        }

    }

    @Override
    protected void onPostExecute(PodcastEntryEntity[] podcastEntries) {
        if(listener != null) {
            listener.onResult(podcastEntries);
        }
    }

    public void setListener(OnApiResultListener listener) {
        this.listener = listener;
    }

    private String getJson(InputStreamReader inputStreamReader) throws IOException {

        StringBuilder response  = new StringBuilder();

        BufferedReader input = new BufferedReader(inputStreamReader);
        String strLine = null;
        while ((strLine = input.readLine()) != null)
        {
            response.append(strLine);
        }
        input.close();

        String json = response.toString();

        int indexOfBegin = json.indexOf("entries") + 9;
        json = json.substring(indexOfBegin, json.length());
        int indexOfEnd = json.indexOf("responseDetails") - 5;
        json = json.substring(0, indexOfEnd);

        return json;
    }

}




Java Source Code List

com.tassioauad.androiditunesclientlibrary.ApplicationTest.java
com.tassioauad.androiditunesclientlibrary.ApplicationTest.java
com.tassioauad.androiditunesclientlibrary.MainActivity.java
com.tassioauad.androiditunesclientlibrary.core.OnApiResultListener.java
com.tassioauad.androiditunesclientlibrary.core.PodcastApiImpl.java
com.tassioauad.androiditunesclientlibrary.core.PodcastApi.java
com.tassioauad.androiditunesclientlibrary.service.PodcastEntriesAsyncTask.java
com.tassioauad.androiditunesclientlibrary.service.SearchPodcastAsyncTask.java
com.tassioauad.androiditunesclientlibrary.service.entity.Category.java
com.tassioauad.androiditunesclientlibrary.service.entity.Content.java
com.tassioauad.androiditunesclientlibrary.service.entity.MediaGroup.java
com.tassioauad.androiditunesclientlibrary.service.entity.PodcastEntity.java
com.tassioauad.androiditunesclientlibrary.service.entity.PodcastEntryEntity.java
com.tassioauad.androiditunesclientlibrary.service.parameter.BasicParameter.java
com.tassioauad.androiditunesclientlibrary.service.parameter.BasicParametersList.java
com.tassioauad.androiditunesclientlibrary.service.parameter.ParameterInterface.java
com.tassioauad.androiditunesclientlibrary.service.parameter.ParametersListInterface.java
com.tassioauad.androiditunesclientlibrary.service.parameter.UrlConvertionInterface.java
com.tassioauad.androiditunesclientlibrary.service.parameter.key.Key.java
com.tassioauad.androiditunesclientlibrary.service.parameter.key.ParameterKeyInterface.java
com.tassioauad.androiditunesclientlibrary.service.parameter.value.AttributeValue.java
com.tassioauad.androiditunesclientlibrary.service.parameter.value.EntityValue.java
com.tassioauad.androiditunesclientlibrary.service.parameter.value.MediaValue.java
com.tassioauad.androiditunesclientlibrary.service.parameter.value.ParameterValueInterface.java
com.tassioauad.androiditunesclientlibrary.service.utils.ItunesSearchApiUrlBuilder.java