How to get String in response using Retrofit 2? Retrofit Android example

Today we are going to look at another awesome library Retrofitto make the http calls. Retrofit is denitely the better alternative to volley in terms of ease of use, performance, extensibility and other things. It is a type-­safe REST client for Android built by Square. Using this tool android developer can make all network stuff much more easier. As an example, we are going to download some json and show it in RecyclerView as a list.

retrofit get api example
Retrofit get api example

 Follow those steps:-

1.Add the permission to access internet in the AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

    <uses-permission android:name="android.permission.INTERNET"/>

        <activity android:name=".activity.MainActivity">
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>



2.Open build.gradle and add Retrofit, Gson dependencies

dependencies {

    implementation ''
//retrofit, gson
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation ''
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.1.0' 

First, we need to know what type of JSON response we will be receiving

 3.Creating Model Class

public class Test {
    private Integer userId;
    private Integer id;
    private String title;
    private String body;

    public Integer getUserId() {
        return userId;

    public void setUserId(Integer userId) {
        this.userId = userId;

    public Integer getId() {
        return id;

    public void setId(Integer id) { = id;

    public String getTitle() {
        return title;

    public void setTitle(String title) {
        this.title = title;

    public String getBody() {
        return body;

    public void setBody(String body) {
        this.body = body;

4. Let’s see how our class looks like

public interface APIInterface {

Call<String> getAllList();

5.Create Setting Up the Retrofit Interface
public class APIClient {
    private static String BASE_URL="";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {

        retrofit = new Retrofit.Builder()

        return retrofit;



public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
private ArrayList<Test> arrTest;

public MyAdapter(Context context, ArrayList<Test> arrTest) {
this.context = context;
this.arrTest = arrTest;


public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_items, parent, false);
return new MyViewHolder(view);

public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Test test= arrTest.get(position);


public int getItemCount() {
return arrTest.size();

public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title,desc;

public MyViewHolder(View itemView) {
public class MainActivity extends AppCompatActivity {
private ArrayList<Test> arrTest;
private Activity activity;
private RecyclerView recyclerView;
private MyAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {

private void getAllList() {
APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
Call<String> call = apiInterface.getAllList();
call.enqueue(new Callback<String>() {
@Override public void onResponse(Call<String> call, Response<String> response) {
if (response != null) {
String data = response.body();
Type type = new TypeToken<ArrayList<Test>>() {}.getType();
arrTest = new Gson().fromJson(data, type);

@Override public void onFailure(Call<String> call, Throwable t) {


private void initRecycler() {
recyclerView = (RecyclerView) findViewById(;
LinearLayoutManager mLayout = new LinearLayoutManager(activity);
adapter = new MyAdapter(activity, arrTest);


Disqus Comments