Repository: https://github.com/square/retrofit
What will I Learn?
In this tutorial, you will learn the following:
Requirements
To successfully complete this tutorial, you will need the following:
Difficulty
Tutorial Content
Retrofit is A type-safe REST client for Android and Java, according to the official page Retrofit Retrofit makes use of annotations to describe HTTP requests, URL parameter replacement and query parameter support is integrated by default. In later tutorials we’ll look at some of the other cool features of retrofit in more detail.
Step 1 : Set Up Your Android Project
To begin this tutorial, lets first a new project with the android studio IDE
Step 2 : Define gradle dependency
build.gradle.dependencies {
// Retrofit
compile 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
}
Step 3 : Set Android’s Network Permission
Retrofit makes an internet request to an API (GitHub) which is running in a server. Making such request from an android application, requires the Internet Permission . This permission is defined within the AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />
Step 4 : And ListView to the activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nexdev.enyason.retrofitgithub.MainActivity">
<ListView
android:id="@+id/repo_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pb_main"/>
</RelativeLayout>
Step 5 : Create a Row Item Layout
Each item from the response is display using this layout
Step 6 : Create a Custom ArrayAdapter
This custom array adapter is to help populate the listview with data. Having a custom adapter makes customization easy for us.We can have a complicated row view and use java Objects as our list Type.
Step 7 :Describe API Endpoints
Before we can start making requests, we need to describe the API endpoints we want to interact with. In this tutorial we’ll just describe a simple GitHub endpoint. First, you have to create an interface and define required methods.
public class GitHubRepo {
String name;
public String getName() {
return name;
}}
public interface GitHubUser {
@GET("/users/{user}/repos")
Call<List<GitHubRepo>> repoForUser(@Path("user") String user);
}
Step 8 : Create Retrofit REST Client from the Retrofit Class
After describing the API interface and the object model, it’s time to prepare an actual request. To do this, we use Retrofit class. After that we use a builder to set some general options for all requests, i.e. the base URL or the converter.
// set up retrofit builder object
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create());
//create retrofit object
Retrofit retrofit = builder.build();
//instance of our github user
GitHubUser gitHubUser = retrofit.create(GitHubUser.class);
Step 9 : Do the Actual Network Request
Here you also use your user object. However, here you don’t pass your callback as the last parameter. You use the client to get a call object. Once you’ve invoked .enqueue on the created call object the request will be made by Retrofit. This request is done asynchronously in order not to block the UI Thread.
@Override
public void onResponse(Call<List<GitHubRepo>> call, Response<List<GitHubRepo>> response) {
progressBar.setVisibility(View.INVISIBLE);
// return the response to a list object
List<GitHubRepo> list = response.body();
MyArrayAdapter arrayAdapter = new MyArrayAdapter(MainActivity.this,list);
// setup the adapter
repoListView.setAdapter(arrayAdapter); // attach adapter to list view
}
@Override
public void onFailure(Call<List<GitHubRepo>> call, Throwable t) {
progressBar.setVisibility(View.INVISIBLE);
}
});
Application Demo
Proof of Work Done
The source code for this tutorial is found here gitHub