RETROFIT TUTORIAL:HOW TO FETCH DATA FROM THE URL USING THE RETROFIT LIBRARY

Retrofit is a networking library which is used to create the HTTP request and get the HTTP response from the REST based web service.

Retrofit is the best alternate of volley.It is also the faster networking library.

Retrofit is developed by the Square.It can be used with the Picasso and glide for the image loading .

Now understand with the help of Example:

Note:

First open the Androidmanifest.xml and put the below code:

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

Step 1:

Suppose we have given the JSON data in the Student_URL=”https://placementpreparation.com/record/student”;

{
"student":
[ 
{
"name":"suman kumar","age":"24"
},
{
"name":"rahul kumar","age":"22"
},
{
"name":"raj kumar","age":"23
},
{
"name":"amit kumar","age":"20"
}
]
}

Step 2:

Open your build.gradle and put the below code:

dependencies
{
implementation 'com.google.code.gson:gson:2.6.2'
implementation 'com.squareup.retrofit2:retrofit:'2.3.0'
implementation 'com.squareup.retrofit2:converter-json:2.2.0'

}

Step 3:

Open the res/layout/student_information.xml and put the below code:

This layout is used for the name and age of the student in the Listview.

student_information.xml

<?xml version="1.0" encoding="utf-8" ?>
<RlativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="mathch_parent"
android:layout_height="match_parent"
android:Orientation="vertical"/>

<TextView
android:id="@+id/student_name"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:id="@+id/student_age"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</Relativelayout>

Step 4:

Opent the res/layout/student_list and put the below code:

<?xml version="1.0" encoding="utf-8" ?>
<RlativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="mathch_parent"
android:layout_height="match_parent"
android:Orientation="vertical"/>

<ListView
android:id="@+id/studentlist"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

Step 5:

Make the Response class in which we will get the JSON data.

StudentResponse.java

import java.util.ArrayList;
import java.util.List;
public class Student
{
@SerializedName("name")
private String Name;
@SerializedName("age")
private String Age;

public String getName()
{
return Name;
}
public void setName(String Name)
{
this.Name=Name;
}

public String getAge()
{
return Age;
}
public void setAge(String Age)
{
this.Age=Age;
}
}

Step 6:

Now Creating the API Interface in which we can send the request and get the response.

InterfaceApi.java

import java.util.List;
public class InterfaceApi
{
private String Base_Url="http://placementpreparation.com/record";

@GET("/student")
Call<StudentResponse> getStudentDetails();
}

The above code contains the Base_Url and annotation of @GET(“student_details”) which add to the Base_Url to give the student details.

Step 7:

Now will get the Response from the given Url .This is the last step.

Here we will create the Retrofit object

Retrofit retrofit=new Retrofit.Builder();
         .baseUrl(InterfaceApi.Base_Url);
         .addConverterFactory(GsonConverterFactory.create());
         .build();

//calling the interface
InterfaceApi interfaceapi=retrofit.create(InterfaceApi.class);

Call<StudentResponse> call=interfaceapi.getStudentDetails();

call.enqueue(new Callback<StudentResponse>()
{
@Override
public void onResponse(Call<StudentResponse> call,Response<StudentResponse> response)
{
List<StudentResponse> student=response.body();

//Here we will get the student details.Here we can set item into the listview
}

@Override
public void onFailure(Call<StudentResponse> call,Throwable t)
{
//if any error occur then we show here
}

Step 8:

Here is the complete code of the retrofit to show the data in the listview.

MainActivity.java

public class MainActivity extends Activity
{
private Listview listview;
@Override
protected void onCreate(Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.student_list);
Private Listview listview;
listview=findViewById(R.id.studentlist);

}
Private void getStudentDetailsApi()
{
Retrofit retofit=new Retrofit.Builder();
             .baseUrl(base_url);
             .addConverterFactory(GsonConverterfactory.create());
             .build();
InterfaceApi interfaceapi=retrofit.create(InterfaceApi.class);

interfaceapi.enqueue(new Callback<Studentresponse>()
{
@Override
Public void onResponse(Call<Studentresponse> call,Response<StudentResponse> response)

                List<StudentResponse> stu_info = response.body();
                listview.setAdapter(new StudentAdapter(stu_info, R.layout.student_information, getApplicationContext()));
}
@Override
Public void onResponse(Call<Studentresponse> call,Throwable t)
{
//whenever error occur then it will execute


private class StudentAdapter extends BaseAdapter
{
Context context;
List<StudentResponse> studentresponse;
 public StudentAdapter(Context context,List<StudentResponse
> studentresponse)
{
this.context=context;
this.studentresponse=studentresponse;
}
@Overide
public int getCount()
{
return studentresponse.size();
}
@Override
public object getItem()
{
return studentresponse.get(position);
}
@Override
public long getItemId()
{
return studentresponse.indexOf(studentresponse.get(position));
}
@Override
public View getView(int position,View convertView,ViewGroup parent)
{
if(convertView==null)
{
LayoutInflater inflater=LayoutInflater.from(getContext());
convertView=inflater.inflate(R.layout.student_information,false);
Textview name=(Textview)convertView.findViewById(R.id,student_name);
Textview age=(Textview)convertView.findViewById(R.id,student_age);
}
return convertView;
}
}
}

Step 9:

The output of the above code:

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *