In this tutorial, we are going to see how to use Retrofit HTTP client in your Android application.
Retrofit is an awesome type-safe HTTP client for Android and Java built by awesome folks at Square. Retrofit makes it easy to consume JSON or XML data which is parsed into Plain Old Java Objects (POJOs).
So, without any further delays, lets get started by first creating a new project in Android Studio.
1.Go to File ⇒ New Project. When it prompts you to select the default activity, select Empty Activity and proceed.
2.Open build.gradle in (Module:app) and add Retrofit, Picasso, RecyclerView, Gson dependencies like this.
dependencies { ... compile "com.android.support:cardview-v7:26.1.0" compile 'com.android.support:recyclerview-v7:26.1.0' compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0' ... }
3.Don’t forget to add INTERNET permissions in AndroidManifest.xml file like this
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tuts.prakash.retrofittutorial"> <!--Internet Permission--> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".activity.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
4. Next, we will create data model to parse our sample JSON data with following structure.
Create a class named RetroPhoto.java under model package like this.
5. Create the Retrofit Instance
To issue network requests to a REST API with Retrofit, we need to create an instance using the Retrofit.Builder class and configure it with a base URL.
Create a class RetrofitClientInstance.java under network package. Here BASE_URL is the basic URL of our API where we will make a call.
public class RetrofitClientInstance { private static Retrofit retrofit; private static final String BASE_URL = "https://jsonplaceholder.typicode.com"; public static Retrofit getRetrofitInstance() { if (retrofit == null) { retrofit = new retrofit2.Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } }
6. Define the Endpoints
The endpoints are defined inside of an interface using special retrofit annotations to encode details about the parameters and request method.
public interface GetDataService { @GET("/photos") Call<List<RetroPhoto>> getAllPhotos(); }
7. Create custom adapter for binding data with RecycleView.
Create a class named CustomAdapter.java under adapter package like this.
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> { private List<RetroPhoto> dataList; private Context context; public CustomAdapter(Context context,List<RetroPhoto> dataList){ this.context = context; this.dataList = dataList; } class CustomViewHolder extends RecyclerView.ViewHolder { public final View mView; TextView txtTitle; private ImageView coverImage; CustomViewHolder(View itemView) { super(itemView); mView = itemView; txtTitle = mView.findViewById(R.id.title); coverImage = mView.findViewById(R.id.coverImage); } } @Override public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); View view = layoutInflater.inflate(R.layout.custom_row, parent, false); return new CustomViewHolder(view); } @Override public void onBindViewHolder(CustomViewHolder holder, int position) { holder.txtTitle.setText(dataList.get(position).getTitle()); Picasso.Builder builder = new Picasso.Builder(context); builder.downloader(new OkHttp3Downloader(context)); builder.build().load(dataList.get(position).getThumbnailUrl()) .placeholder((R.drawable.ic_launcher_background)) .error(R.drawable.ic_launcher_background) .into(holder.coverImage); } @Override public int getItemCount() { return dataList.size(); } }
7. Final step
Inside the onCreate() method of the MainActivity.java, we initialize an instance of the GetDataService interface (line 16), the RecyclerView, and also the adapter. Finally, we call the generateDataList() method.
public class MainActivity extends AppCompatActivity { private CustomAdapter adapter; private RecyclerView recyclerView; ProgressDialog progressDoalog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressDoalog = new ProgressDialog(MainActivity.this); progressDoalog.setMessage("Loading...."); progressDoalog.show(); /*Create handle for the RetrofitInstance interface*/ GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class); Call<List<RetroPhoto>> call = service.getAllPhotos(); call.enqueue(new Callback<List<RetroPhoto>>() { @Override public void onResponse(Call<List<RetroPhoto>> call, Response<List<RetroPhoto>> response) { progressDoalog.dismiss(); generateDataList(response.body()); } @Override public void onFailure(Call<List<RetroPhoto>> call, Throwable t) { progressDoalog.dismiss(); Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show(); } }); } /*Method to generate List of data using RecyclerView with custom adapter*/ private void generateDataList(List<RetroPhoto> photoList) { recyclerView = findViewById(R.id.customRecyclerView); adapter = new CustomAdapter(this,photoList); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(adapter); } }
8. Understanding enqueue()
enqueue() asynchronously sends the request and notifies your app with a callback when a response comes back. Since this request is asynchronous, Retrofit handles it on a background thread so that the main UI thread isn’t blocked or interfered with.
To use enqueue(), you have to implement two callback methods:
onResponse()
onFailure()