Android Open Source - Android-iTunes-Client-Library Search Podcast 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;
/* www  .j a v a  2 s .  com*/
import android.os.AsyncTask;

import com.tassioauad.androiditunesclientlibrary.core.OnApiResultListener;
import com.tassioauad.androiditunesclientlibrary.service.entity.PodcastEntity;
import com.tassioauad.androiditunesclientlibrary.service.parameter.BasicParameter;
import com.tassioauad.androiditunesclientlibrary.service.parameter.ParameterInterface;
import com.tassioauad.androiditunesclientlibrary.service.parameter.ParametersListInterface;
import com.tassioauad.androiditunesclientlibrary.service.parameter.key.Key;
import com.tassioauad.androiditunesclientlibrary.service.parameter.value.EntityValue;
import com.tassioauad.androiditunesclientlibrary.service.utils.ItunesSearchApiUrlBuilder;
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 21/05/2014.
 */
public class SearchPodcastAsyncTask extends AsyncTask<ParametersListInterface, Void, PodcastEntity[]> {

    private OnApiResultListener listener;

    @Override
    protected PodcastEntity[] doInBackground(ParametersListInterface... parametersListInterfaces) {

        ParametersListInterface parametersListInterface = parametersListInterfaces[0];

        try {
            String url = getUrl(parametersListInterface);

            OkHttpClient client = new OkHttpClient();

            Request request = new Request.Builder().url(url).build();

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

            String jsonResponsed = getJson((InputStreamReader) response.body().charStream());

            return new Gson().fromJson(jsonResponsed, PodcastEntity[].class);

        } catch (Exception e) {
            return new PodcastEntity[0];
        }

    }

    @Override
    protected void onPostExecute(PodcastEntity[] 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 indexOf = json.indexOf("[") ;
        json = json.substring(indexOf, json.length());
        json = json.substring(0, json.length()-1);

        return json;
    }

    private String getUrl(ParametersListInterface parametersListInterface) {
        boolean thereIsPodcastParameter = false;
        for(ParameterInterface parameterInterface : parametersListInterface.getList()) {
            if(parameterInterface.getKey().equals(Key.ENTITY.getValue())
                    && parameterInterface.getValue().equals(EntityValue.POSCAST.getValue())) {
                thereIsPodcastParameter = true;
            }

        }

        if(!thereIsPodcastParameter) {
            parametersListInterface.add(new BasicParameter(EntityValue.POSCAST));
        }

        return ItunesSearchApiUrlBuilder.build(parametersListInterface);
    }
}




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