The movie db api android
A simple Android client for The Movie DB
This project is an Android app which displays data from The Movie Database The Movie Database API.
- Get Movies
- Get the Popular Movies
- Get the Top Rated Movies
- Get the Upoming
- Get the Now Playing
- Get Tv shows
- Get the Popular Tv shows
- Get the Top Rated
- Get the On Tv
- Get the Airing Today
- Get People
- Show the movie detail and tv shows details
- Get the images from a movie and tv shows
- Get the trailers/clips from a movie and tv show
- Search Movies, tv shows and persons
Build this project
In the build.gradle assign your API_KEY_TMDB
- Get the TheMoviedb API KEY here
- Fully written in Kotlin language
- Retrofit 2
- Dagger 2
- RxKotlin 2
- RxAndroid 2
- DraggablePanel — DEPRECATED
- QSVideoPlayer
- Gson
- Glide
- MaterialSearchView
- KAndroid
- Android Ktx
- Android View Animations
- Circle-Progress-View
- Other support libraries Androidx — AppCompat, RecyclerView, CardView, Design
About
A simple Android client for The Movie DB, implementation Androidx, Dagger2, Retrofit2, RxJava2-RxKotlin
Источник
The movie db api android
This is an Android app that interacts with the TMDB API (http://developers.themoviedb.org/). It uses Android Architecture Components with Dagger 2.
The app is composed of a single main screen where a list of popular movies is shown as soon as the app launches. The user has the possibility to search for a movie by keyword. The search is automatically executed while typing the search term. When the user continues typing, the old search is cancelled and a search for the new term is started.
Allows you to search movies on TMDB. Each search result is kept in the database in MovieSearchResult table where the list of movie IDs are denormalized into a single column. The actual Movie instances live in the Movie table.
Each time a new page is fetched, the same MovieSearchResult record in the Database is updated with the new list of movie ids.
You can open the project in Android studio and press run.
The project uses both instrumentation tests that run on the device and local unit tests that run on your computer. To run both of them and generate a coverage report, you can run:
./gradlew fullCoverageReport (requires a connected device or an emulator)
The projects uses Espresso for UI testing. Since each fragment is limited to a ViewModel, each test mocks related ViewModel to run the tests.
The project creates an in memory database for each database test but still runs them on the device.
Local Unit Tests
Each ViewModel is tested using local unit tests with mock Repository implementations.
Each Repository is tested using local unit tests with mock web service and mock database.
The project uses MockWebServer project to test REST api interactions.
- Android Support Library
- Android Architecture Components
- Android Data Binding
- Dagger 2 for dependency injection
- Retrofit for REST api communication
- Glide for image loading
- Timber for logging
- espresso for UI tests
- mockito for mocking in tests
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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
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.
Источник
The movie db api android
The movies DB API Android Library Module. This repository contains an Android Module for ingesting data from TheMoviesDB. This module was part of the Popular Movie Application that I had made for Android Nanodegree Program.
This library requires Android 2.3 and uses java 7
One of the common issues is duplication of Licence/Notice file due to one of the dependencies. Here is a quick fix for it:
The TMDBApi is provided within the library and this is used to query the various endpoints. As of now, only a limited number of urls are available to query. These urls are just enough for the Android Nanodegree Program. All you need to make use of this library is your TMDB api key and your good to go.
The above two lines of code is enough to create the api. Using the tmdbApi you can now query the various urls that is available in the API.
Here is a small snipped that lets you query to the discover API
The api also gives call to the genres endpoint to get a list of all the genres.
This api also gives call to the video endpoint in order to get the video of a specific movie
This api gives the ability to sort based on following categories
- popularity(«popularity.desc»),
- vote_average(«vote_average.desc»),
- vote_count(«vote_count.desc»),
- original_title(«original_title.desc»),
- primary_release_date(«primary_release_date.desc»),
- revenue(«revenue.desc»),
- release_date(«release_date»);
The Sort class is particularly useful when querying the discover movies endpoint. This gives the ability to sort on various parameters.
This library also supports call to the review endpoint for getting specific movies review
In order to create a synchronous call, you can exeucte the following snippet of code
This needs to be done in the initial creating of the object. All subsequent calls to the api wil now be synchronous
Источник
Consuming REST API using Retrofit Library in Android
Mar 19, 2017 · 10 min read
Hi Android beginner developers, in this tutorial, i’ll introduce you to the Retrofit Library and also try to provide a step by step guide on how to consume JSON objects using the retrofit library. This practice requires that you are familiar with Java and Android App development. For those interested in learning the very basics of Java, check my blog for some very old articles i wrote on Introduction to Java
In this tutorial, we are goin g to create an Android App that uses the Retrofit Library to download JSON Objects containing movie details from The Movie DB API, this movie details will now be displayed in a RecyclerView on the Android App.
Find the full source code for the project here on GitHub.
What is Retrofit?
Retrofit is a REST Client library (Helper Library) used in Android and Java to create an HTTP request and also to process the HTTP response from a REST API. It was created by Square, you can also use retrofit to receive data structures other than JSON, for example SimpleXML and Jackson. Before we continue, let’s briefly define REST Client and REST API in our context.
REST Client in our case is the Retrofit library that is used on the client side (Android) to make HTTP request to REST API, in our case, The Movie DB API and also process the response.
A REST API defines a set of functions which developers can perform requests and receive responses via HTTP protocol such as GET and POST. in our case, The Movie DB (TMDB) API is the REST API.
We can also simply say that a RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data.
To use Retrofit in your Android Application, you’ll need 3 major classes.
- An Interface which defines the HTTP operations (Functions or methods)
According to Square, creators of Retrofit documentation, Retrofit turns your HTTP API into a Java interface. Sample codes for the interface and the method declared in it are as below:
Every method inside an interface represents one possible API call. It must have a HTTP annotation (GET, POST, etc.) to specify the request type and the relative URL. The return value wraps the response in a Call object with the type of the expected result.
Query parameters can also be added to a method.
You can use replacement blocks and query parameters to adjust the URL. A replacement block is added to the relative URL with <>. With the help of the @Path annotation on the method parameter, the value of that parameter is bound to the specific replacement block.
2. A Retrofit class which generates an implementation of the GitHubService interface. The below sample code would be inside the Retrofit class and this is how it creates an instance of Retrofit and implements the listRepos() method that’s in the GitHubService Interface.
3. The last of the 3 needed class is a simple POJO that matches each field in the JSON response object gotten from querying an API. It’s a simple class with getter and setter methods for each fields. We’ll see sample codes later.
Retrofit Converters are like an agreement between and Android client and the Server on the format on which data will be represented. Both parties can agree that for our communication, the format for data transfer will be JSON, as in our case in this tutorial. Remember i said apart from the JSON structure converter, we have others and here are some supported by Retrofit.
Gson is for JSON mapping and can be added with the following dependency:
SimpleXML is for XML mapping. You’ll need the following line for your build.gradle:
Jackson is an alternative to Gson and claims to be faster in mapping JSON data. The setup offers you a lot more customization and might be worth a look. You can add it with:
Moshi is another alternative to Gson. It’s created by the developers of Retrofit. Moshi is based on Gson, but differentiates itself with some simplifications. If you want to give this young new player on the market a try, add it with:
Retrofit supports Authentication for API calls that require the user to be authenticated before using the API to access resources. Querying Twitter, Facebook and StackoverFlow all require authentication. However, authentication feature is out of the scope of this tutorial as we’ll simply be querying The Movie DB(TMDB) API which requires no authentication.
Hmm! Enough talk, now It’s time to write some codes. Let’s now create an Android App using Android Studio. The App uses Retrofit to fetch Movie details from “The Movie DB” website and then displays these details on the Android Client RecyclerView Activity.
We’ll be using the TMDB API, So the first step is to get the API key from their website. TMDB is a popular website for getting information on movies, it’s a community built movie and TV database. It also provides a REST API that is well documented. Queries can be built using this API. click here to see the API documentation.
Click here for the website’s instructions on how to obtain API key, you just need to register and be logged in.
CREATE ANDROID APP
- Create new project in Android Studio from File, New Project. When it prompts you to select the default activity, select Empty Activity and proceed. Please note that the sample code for this app is on my GitHub Repo, so make sure you check it out for the complete source code and other resources for the project.
- Open your project’s App Build.gradle file and include the following dependencies:
You might be wondering why add recyclerview and picasso dependencies, since we’ll be using Picasso to load image URLs into an imageView and we’ll display the movie details in a RecyclerView, so adding their dependencies at this stage is wise.
3. Since we’ll be querying the TMDB API which is a network operations we’ll need to add INTERNET permissions in AndroidManifest.xml file:
4. Create four sub packages named activity, adapter, rest and model in your main package. Move your empty MainActivity into the activity package.
Remember that after querying TMDB API, a JSON response will be returned. We need to know the exact fields that will be included in the response, so we can create a POJO that will be able to parse the latest movies. For us to know these fields in advance, let’s first define how a basic movie representation should look like
Using this URL (http://api.themoviedb.org/3/movie/top_rated?api_key=INSERT_YOUR_API_KEY) we can get the last 50 movies. Let’s insert it into browser and see. Using JsonViewer you can see JSON in more structured way. Copy the response from browser to this JsonViewer and this online tool show JSON like below:
5. So now that what we have seen the expected fields from the above image which shows the different fields of the JSON response, let’s create a class named Movie.java under model package which is a simple POJO to hold all fields and provide getter and setter methods for fields that we are expecting in the JSON response object. Past the below code in the Movie.java file.
6. We also need to create MovieResponse.java class inside the model package, since we have some extra fields like page number. This class contains all fetched movies and extra information. Create MovieResponse.java under model package and past the below code.
7. Next, let’s define the Interface containing the Endpoint method used to query the TMDB API. Special retrofit annotations are used to encode details about the parameters and request method. the interface defines each endpoint in the following way. Note that we only have one endpoint method in the Interface, in a real programming world scenario, you can have multiple endpoints methods inside the interface. So create an interface named MovieApiService.java and past the below codes.
8. Let’s now create the MainActivty.java class which is the activity from where we make request to the Movie DB API. Open the MainActivity.java and insert in the below codes.
Retrofit will download and parse the API data on a background thread, and then return the results back to the UI thread via the onResponse or onFailure method.
9. It’s time to display the result gotten from querying the API using the above codes on the layout. We’ll need to create a layout which will contain 4 textViews and 2 imageViews.
10. In other to the display the ratings, we need to create the star imageView, so create a layout named star.xml under res drawable with the below content.
11. Open the activity_main.xml file and copy the below code into it.
12. Create a layout named list_item_movie.xml under res layout folder
12. Adapter is a common pattern which helps to bind view and data, so let’s implement adapter for this. Create a class named MoviesAdapter.java under adapter package.
Now save and run the App. You can download the full source code for this project on GitHub
Источник