com.sociablue.nanodegree_p1.managers.NetworkManager.java Source code

Java tutorial

Introduction

Here is the source code for com.sociablue.nanodegree_p1.managers.NetworkManager.java

Source

/*
 * Copyright (c) 2015. Sociablue Labs
 *
 * 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.sociablue.nanodegree_p1.managers;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.sociablue.nanodegree_p1.Constants.AppConstants;
import com.sociablue.nanodegree_p1.Constants.MdbConstants;
import com.sociablue.nanodegree_p1.apiDefinitions.MDB;
import com.sociablue.nanodegree_p1.utils.Utils;

import java.util.Date;
import java.util.HashMap;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.converter.GsonConverter;

/**
 * Created by okikejiani on 2015-08-26.
 * Implements all Rest Calls made to the Web Services. Is an abstraction layer for network calls.
 * TODO: After Nano degree is completed, Improve manager.
 *  1) add network status checking
 *  2) network call retrying on failure.
 *  3) data synchronization with local models.
 *
 */
public class NetworkManager {

    MDB MdbService;

    public NetworkManager() {
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .registerTypeAdapter(Date.class, new DateTypeAdapter()).create();

        RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(MdbConstants.BASE_URL)
                .setConverter(new GsonConverter(gson)).build();

        MdbService = restAdapter.create(MDB.class);

    }

    public void discoverMovies(HashMap<String, Object> params, Callback CB) {
        MdbService.discoverMovie(AppConstants.MDB_API_KEY, Utils.getCurrentFormattedDate("yyyy-MM-dd"),
                (int) params.get(MdbConstants.VOTE_COUNT_GTE), (String) params.get(MdbConstants.SORT_BY), CB);

    }

}