public static final class TVCast implements BaseColumns {

    public static final String TABLE_NAME = "tv_cast";

    public static final String CHARACTER = "character";
    public static final String CREDIT_ID = "credit_id";
    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String ORDER = "order_cast";
    public static final String PROFILE_PATH = "profile_path";
    public static final String TV_ID = "tv_id";

    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_TV_CAST).build();

    static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_CAST;
    static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_CAST;

    static Uri buildTVCastUri(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }

    //content://work....../tv_casts/TvId
    public static Uri buildCastsUriWithTVId(String TVId) {
        return CONTENT_URI.buildUpon().appendPath(TVId).build();
    }

    public static Uri buildCastUri() {
        return CONTENT_URI.buildUpon().build();
    }

    public static String getIDFromUri(Uri uri) {
        return uri.getPathSegments().get(1);
    }

} public static final class TVCreator implements BaseColumns {

    public static final String TABLE_NAME = "tv_creator";

    public static final String TV_ID = "tv_id";
    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String PROFILE_PATH = "profile_path";

    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_TV_CREATOR).build();

    static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_CREATOR;
    static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_CREATOR;

    static Uri buildTVCreatorUri(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }

    //content://work....../tv_creator/tv_id
    public static Uri buildTVCreatorUriWithTVId(String id) {
        return CONTENT_URI.buildUpon().appendPath(id).build();
    }

    public static Uri buildTVCreatorUri() {
        return CONTENT_URI.buildUpon().build();
    }

    public static String getIDFromUri(Uri uri) {
        return uri.getPathSegments().get(1);
    }

} public static final class TVDetails implements BaseColumns {

    public static final String TABLE_NAME = "tv_details";

    public static final String TV_ID = "id";
    public static final String BACKDROP_PATH = "backdrop_path";
    public static final String FIRST_AIR_DATE = "first_air_date";
    public static final String HOMEPAGE = "homepage";
    public static final String IN_PRODUCTION = "in_production";
    public static final String LAST_AIR_DATE = "last_air_date";
    public static final String NAME = "name";
    public static final String NUMBER_OF_EPISODES = "number_of_episodes";
    public static final String NUMBER_OF_SEASONS = "number_of_seasons";
    public static final String ORIGINAL_LANG = "original_language";
    public static final String ORIGINAL_NAME = "original_name";
    public static final String OVERVIEW = "overview";
    public static final String POPULARITY = "popularity";
    public static final String POSTER_PATH = "poster_path";
    public static final String STATUS = "status";
    public static final String TYPE = "type";
    public static final String VOTE_AVERAGE = "vote_average";
    public static final String VOTE_COUNT = "vote_count";
    public static final String FAVOURED = "favoured";

    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_TV_DETAILS).build();

    static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_DETAILS;
    static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_DETAILS;

    static Uri buildTVDetailsUri(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }

    //content://work....../tv_details/tvId
    public static Uri buildTVDetailsUriWithTvId(String TVId) {
        return CONTENT_URI.buildUpon().appendPath(TVId).build();
    }

    public static Uri buildTVDetailsUri() {
        return CONTENT_URI.buildUpon().build();
    }

    public static String getIDFromUri(Uri uri) {
        return uri.getPathSegments().get(1);
    }

} public static final class TVEpisodeRuntime implements BaseColumns {

    public static final String TABLE_NAME = "tv_episode_runtime";

    public static final String TV_ID = "tv_id";
    public static final String TIME = "time";

    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_TV_EPISODE_RUNTIME).build();

    static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_EPISODE_RUNTIME;
    static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_EPISODE_RUNTIME;

    static Uri buildTVEpisodeUri(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }

    //content://work....../tv_episode_runtime/tv_id
    public static Uri buildTVEpisodeRuntimeUriWithTVId(String tv_id) {
        return CONTENT_URI.buildUpon().appendPath(tv_id).build();
    }

    public static Uri buildTVEpisodeRuntimeUri() {
        return CONTENT_URI.buildUpon().build();
    }

    public static String getIDFromUri(Uri uri) {
        return uri.getPathSegments().get(1);
    }

} public static final class TVGenres implements BaseColumns {

    public static final String TABLE_NAME = "tv_genres";

    public static final String ID_GENRES = "id_genres";
    public static final String NAME = "name";
    public static final String TV_ID = "tv_id";


    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_TV_GENRES).build();

    static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_GENRES;
    static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_GENRES;

    static Uri buildTVGenreUri(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }

    //content://work....../genres/tvId
    public static Uri buildGenresUriWithTVId(String tvId) {
        return CONTENT_URI.buildUpon().appendPath(tvId).build();
    }

    public static Uri buildGenreUri() {
        return CONTENT_URI.buildUpon().build();
    }

    public static String getIDFromUri(Uri uri) {
        return uri.getPathSegments().get(1);
    }

} public static final class TVNetworks implements BaseColumns {

    public static final String TABLE_NAME = "tv_networks";

    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String TV_ID = "tv_id";


    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_TV_NETWORKS).build();

    static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_NETWORKS;
    static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_NETWORKS;

    static Uri buildTVNetworksUri(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }

    //content://work....../tv_networks/tvId
    public static Uri buildTVNetworksUriWithTVId(String id) {
        return CONTENT_URI.buildUpon().appendPath(id).build();
    }

    public static Uri buildGenreUri() {
        return CONTENT_URI.buildUpon().build();
    }

    public static String getIDFromUri(Uri uri) {
        return uri.getPathSegments().get(1);
    }

} public static final class TVSeasons implements BaseColumns {

    public static final String TABLE_NAME = "tv_seasons";

    public static final String ID = "id";
    public static final String EPISODE_COUNT = "episode_count";
    public static final String TV_ID = "tv_id";
    public static final String SEASON_NUMBER = "season_number";
    public static final String POSTER_PATH = "poster_path";


    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_TV_SEASONS).build();

    static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_SEASONS;
    static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_SEASONS;

    static Uri buildTVSeasonsUri(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }

    //content://work....../tv_seasons/tvId
    public static Uri buildTVSeasonsUriWithTVId(String tvId) {
        return CONTENT_URI.buildUpon().appendPath(tvId).build();
    }

    public static Uri buildGenreUri() {
        return CONTENT_URI.buildUpon().build();
    }

    public static String getIDFromUri(Uri uri) {
        return uri.getPathSegments().get(1);
    }

} public static final class TVSimilar implements BaseColumns {

    public static final String TABLE_NAME = "similar_tv";
    public static final String TV_ID_OLD = "id_old";
    public static final String PAGE = "page";
    public static final String POSTER_PATH = "poster_path";
    public static final String OVERVIEW = "overview";
    public static final String FIRST_AIR_DATE = "first_air_date";
    public static final String TV_ID = "id";
    public static final String ORIGINAL_NAME = "original_name";
    public static final String ORIGINAL_LANGUAGE = "original_language";
    public static final String NAME = "name";
    public static final String BACKDROP_PATH = "backdrop_path";
    public static final String POPULARITY = "popularity";
    public static final String VOTE_COUNT = "vote_count";
    public static final String VOTE_AVERAGE = "vote_average";
    public static final String FAVOURED = "favoured";

    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_TV_SIMILAR).build();

    static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_SIMILAR;
    static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_SIMILAR;

    static Uri buildSimilarTVUri(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }

    //content://work....../similar_tv/MovieId
    public static Uri buildSimilarTVUriWithTVId(String TVId) {
        return CONTENT_URI.buildUpon().appendPath(TVId).build();
    }

    public static Uri buildSimilarTVUri() {
        return CONTENT_URI.buildUpon().build();
    }

    public static String getIDFromUri(Uri uri) {
        return uri.getPathSegments().get(1);
    }
} public static final class TVVideos implements BaseColumns {

    public static final String TABLE_NAME = "tv_videos";

    public static final String VIDEO_ID = "id";
    public static final String KEY = "key";
    public static final String NAME = "name";
    public static final String SITE = "site";
    public static final String SIZE = "size";
    public static final String TYPE = "type";
    public static final String TV_ID = "tv_id";

    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_TV_VIDEOS).build();

    static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_VIDEOS;
    static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TV_VIDEOS;

    static Uri buildTVVideosUri(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }

    //content://work....../tv_videos/TvId
    public static Uri buildTVUriWithTVId(String tvId) {
        return CONTENT_URI.buildUpon().appendPath(tvId).build();
    }

    public static Uri buildTVVideosUri() {
        return CONTENT_URI.buildUpon().build();
    }

    public static String getIDFromUri(Uri uri) {
        return uri.getPathSegments().get(1);
    }

} public interface AsyncResponse {
    void onFinish(boolean isData);
}
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector;
import work.technie.popularmovies.BuildConfig;
import work.technie.popularmovies.data.MovieContract.TVCast;
import work.technie.popularmovies.data.MovieContract.TVCreator;
import work.technie.popularmovies.data.MovieContract.TVDetails;
import work.technie.popularmovies.data.MovieContract.TVEpisodeRuntime;
import work.technie.popularmovies.data.MovieContract.TVGenres;
import work.technie.popularmovies.data.MovieContract.TVNetworks;
import work.technie.popularmovies.data.MovieContract.TVSeasons;
import work.technie.popularmovies.data.MovieContract.TVSimilar;
import work.technie.popularmovies.data.MovieContract.TVVideos;
import work.technie.popularmovies.utils.AsyncResponse;
/*
 * Copyright (C) 2017 Anupam Das
 *
 * 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 work.technie.popularmovies.asyntask;





/**
 * Created by anupam on 24/12/15.
 */
public class FetchTVDetail extends AsyncTask<String, Void, Void> {

    private final String LOG_TAG = FetchTVDetail.class.getSimpleName();

    private final Context mContext;
    public AsyncResponse response = null;
    private boolean DEBUG = true;

    public FetchTVDetail(Context context) {
        mContext = context;
    }

    /**
     * Take the String representing the complete tv list in JSON Format and
     * pull out the data we need to construct the Strings needed for the wireframes.
     * <p>
     * Fortunately parsing is easy:  constructor takes the JSON string and converts it
     * into an Object hierarchy for us.
     */
    private void gettvDataFromJson(String tvJsonStr)
            throws JSONException {

        // These are the names of the JSON objects that need to be extracted.
        final String NAME = "name";
        final String GENRE_ID = "id";
        final String TOTAL_PAGES_REVIEWS = "total_pages";
        final String TOTAL_RESULTS_REVIEWS = "total_results";
        final String PAGE = "page";
        final String tv_ID = "id";

        final String RESULT = "results";
        final String RESULT1 = "videos";
        final String RESULT3 = "genres";
        final String RESULT4 = "similar";
        final String RESULT5 = "seasons";
        final String RESULT6 = "episode_run_time";
        final String RESULT7 = "networks";
        final String CREDITS = "credits";
        final String CREATED_BY = "created_by";
        final String CAST = "cast";
        final String POSTER_BASE_URL = "http://image.tmdb.org/t/p/w185";
        final String BACKDROP_BASE_URL = "http://image.tmdb.org/t/p/w500";


        try {
            JSONObject tvJson = new JSONObject(tvJsonStr);
            JSONArray tvArrayVideos = tvJson.getJSONObject(RESULT1).getJSONArray(RESULT);
            JSONArray tvArrayCast = tvJson.getJSONObject(CREDITS).getJSONArray(CAST);
            JSONArray tvArrayCreator = tvJson.getJSONArray(CREATED_BY);

            JSONObject tvArraySimilar = tvJson.getJSONObject(RESULT4);
            JSONArray tvArrayGenres = tvJson.getJSONArray(RESULT3);
            JSONArray tvArraySeasons = tvJson.getJSONArray(RESULT5);
            JSONArray tvArrayNetworks = tvJson.getJSONArray(RESULT7);
            JSONArray tvArrayEpisodeRuntime = tvJson.getJSONArray(RESULT6);

            String tv_id = tvJson.getString(tv_ID);
            Vector<ContentValues> cVVectorEpisodeRuntime = new Vector<ContentValues>(tvArrayEpisodeRuntime.length());

            for (int i = 0; i < tvArrayEpisodeRuntime.length(); i++) {

                String runtime = tvArrayEpisodeRuntime.getString(i);

                ContentValues tvValues = new ContentValues();
                // Then add the data, along with the corresponding name of the data type,
                // so the content provider knows what kind of value is being inserted.

                tvValues.put(TVEpisodeRuntime.TV_ID, tv_id);
                tvValues.put(TVEpisodeRuntime.TIME, runtime);
                cVVectorEpisodeRuntime.add(tvValues);

            }
            // add to database
            if (cVVectorEpisodeRuntime.size() > 0) {
                ContentValues[] cvArray = new ContentValues[cVVectorEpisodeRuntime.size()];
                cVVectorEpisodeRuntime.toArray(cvArray);
                mContext.getContentResolver().bulkInsert(TVEpisodeRuntime.CONTENT_URI, cvArray);
            }

            Vector<ContentValues> cVVectorTrailer = new Vector<ContentValues>(tvArrayVideos.length());

            for (int i = 0; i < tvArrayVideos.length(); i++) {

                JSONObject trailerInfo = tvArrayVideos.getJSONObject(i);

                String video_id;
                String key;
                String name;
                String site;
                String size;
                String type;


                video_id = trailerInfo.getString(TVVideos.VIDEO_ID);
                key = trailerInfo.getString(TVVideos.KEY);
                name = trailerInfo.getString(TVVideos.NAME);
                site = trailerInfo.getString(TVVideos.SITE);
                size = trailerInfo.getString(TVVideos.SIZE);
                type = trailerInfo.getString(TVVideos.TYPE);

                if (!site.trim().equalsIgnoreCase("youtube")) {
                    continue;
                }

                ContentValues tvValues = new ContentValues();

                // Then add the data, along with the corresponding name of the data type,
                // so the content provider knows what kind of value is being inserted.

                tvValues.put(TVVideos.VIDEO_ID, video_id);
                tvValues.put(TVVideos.KEY, key);
                tvValues.put(TVVideos.NAME, name);
                tvValues.put(TVVideos.SITE, site);
                tvValues.put(TVVideos.SIZE, size);
                tvValues.put(TVVideos.TYPE, type);
                tvValues.put(TVVideos.TV_ID, tv_id);
                cVVectorTrailer.add(tvValues);
            }
            // add to database
            if (cVVectorTrailer.size() > 0) {
                ContentValues[] cvArray = new ContentValues[cVVectorTrailer.size()];
                cVVectorTrailer.toArray(cvArray);
                mContext.getContentResolver().bulkInsert(TVVideos.CONTENT_URI, cvArray);
            }

            Vector<ContentValues> cVVectorSeasons = new Vector<ContentValues>(tvArraySeasons.length());

            for (int i = 0; i < tvArraySeasons.length(); i++) {

                JSONObject gen = tvArraySeasons.getJSONObject(i);
                String id = gen.getString(TVSeasons.ID);