Retrofit Android:-This Tutorial is Develop to describe how to get or fetch data from server with Retrofit in Retrofit is latest Restful Api that make data accessibility more easy and fast Earlier i i have discussed about post data using retrofit let's see how can be fetching data with Retrofit achieved in Retrofit android topic so let's start step by step and it;s 100% working
Step-1 : Create a Project Define MainActivity as you want like this. This is may be your main activity in my Activity name is as following first You just Create
at first You Create Layouts For this :
activity_getusing.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:shimmer="http://schemas.android.com/apk/res-auto"
tools:context="com.example.rajeev.getandpostusingratrofit.GetUsingActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:paddingBottom="0dp"
android:divider="#faf8f8"
android:dividerHeight="2dp"
android:layout_alignParentStart="true"
/>
</android.support.constraint.ConstraintLayout>
Now create list of like below
patientlist.java
package com.example.rajeev.getandpostusingratrofit;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
/**
* Created by RAJEEV on 03-Aug-18.
*/
public class Patientlist {
@SerializedName("patients")
private ArrayList<PatientModel> patient=new ArrayList<>();
public ArrayList<PatientModel> getPatientArrayList() {
return patient;
}
public void setPatient(ArrayList<PatientModel> patient) {
this.patient = patient;
}
}
patientlist.xml
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.design.widget.CoordinatorLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="290dp" | |
android:background="#890c0c" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/ids" | |
android:text="id" | |
android:textColor="#FFFFFF" | |
android:textSize="25dp"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/name" | |
android:textColor="#FFFFFF" | |
android:text="name" | |
android:textSize="25dp"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/blg" | |
android:textColor="#FFFFFF" | |
android:text="Blood group" | |
android:textSize="25dp"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/contact" | |
android:textColor="#FFFFFF" | |
android:text="contact number" | |
android:textSize="25dp"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/address" | |
android:text="address" | |
android:textColor="#FFFFFF" | |
android:textSize="25dp"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/donar" | |
android:text="donar" | |
android:textColor="#FFFFFF" | |
android:textSize="25dp"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/city" | |
android:text="city" | |
android:textColor="#FFFFFF" | |
android:textSize="25dp"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/time" | |
android:text="time" | |
android:textColor="#FFFFFF" | |
android:textSize="25dp"/> | |
</LinearLayout> | |
</android.support.design.widget.CoordinatorLayout> |
Before Start any other work first of all you need to create a interface name may be difference but in my case after creating this follow step below to fetch data using Retrofit in android so let do this fun
Service.java
public interface Service {
String BASE1_URL = "http://ramgopalabs.000webhostapp.com/"; //first half of link
@GET("v1/EmergencyRequirement.php/?op=getPatient") //secondhalf of link
Call<Patientlist> getstatus();
}
This class is Used to call api using retrofit it's contain link
GetUsingActivity.java
public class GetUsingActivity extends AppCompatActivity {
List<PatientModel> patientlst;
ListView listView;
FetchAdapter adapter;
ShimmerFrameLayout shimmerFrameLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_using);
shimmerFrameLayout=findViewById(R.id.shimmer_view_container);
listView=(ListView)findViewById(R.id.listview);
patientlst=new ArrayList<>();
loaddata();
}
private void loaddata() {
shimmerFrameLayout.startShimmerAnimation();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Service.BASE1_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
final Service api = retrofit.create(Service.class);
Call<Patientlist> call=api.getstatus();
call.enqueue(new Callback<Patientlist>() {
@Override
public void onResponse(Call<Patientlist> call, Response<Patientlist> response) {
if (response.isSuccessful()){
patientlst=response.body().getPatientArrayList();
adapter=new FetchAdapter(GetUsingActivity.this,R.layout.patientlist,patientlst);
listView.setAdapter(adapter);
}
shimmerFrameLayout.stopShimmerAnimation();
shimmerFrameLayout.setVisibility(View.GONE);
}
@Override
public void onFailure(Call<Patientlist> call, Throwable t) {
}
});
}
@Override
protected void onResume() {
super.onResume();
shimmerFrameLayout.startShimmerAnimation();
}
@Override
protected void onPause() {
super.onPause();
shimmerFrameLayout.stopShimmerAnimation();
}
}
in This class all main fundamental are regarding fetch but some of the work is rest works to do is as following :-
now Create Adapter Class to get and set data
FetchAdapter.java
public class FetchAdapter extends ArrayAdapter{
Context context;
LayoutInflater inflater;
int resource;
List<PatientModel>list;
public FetchAdapter(@NonNull Context context, int resource,List<PatientModel>list) {
super(context, resource,list);
this.context = context;
this.resource = resource;
this.list = list;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(resource,null);
holder = new ViewHolder();
holder.id = (TextView)convertView.findViewById(R.id.ids);
holder.bloodgroup=(TextView)convertView.findViewById(R.id.blg);
holder.address=(TextView)convertView.findViewById(R.id.address);
holder.city=(TextView)convertView.findViewById(R.id.city);
holder.name=(TextView)convertView.findViewById(R.id.name);
holder.contact=(TextView)convertView.findViewById(R.id.contact);
holder.time=(TextView)convertView.findViewById(R.id.time);
holder.donar=(TextView)convertView.findViewById(R.id.donar);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
PatientModel model = list.get(position);
holder.id.setText("Id: "+model.getId());
holder.name.setText("Patient Name: "+model.getName());
holder.bloodgroup.setText("blood group: "+model.getBloodgroup());
holder.address.setText("Address: "+model.getAdress());
holder.city.setText("City: "+model.getCity());
holder.contact.setText("Contact: "+model.getContact());
holder.time.setText("Time: "+model.getTime());
holder.donar.setText("Donar Name: "+model.getDonar());
return convertView;
}
static class ViewHolder{
TextView id,name,bloodgroup,city,contact,address,time,donar;
}
}
Now it's time to create model class Below is the way to create
PatientModel.java
public class PatientModel {
@SerializedName("Id")
@Expose
private String id;
@SerializedName("BloodGroup")
@Expose
private String bloodgroup;
@SerializedName("Address")
@Expose
private String adress;
@SerializedName("City")
@Expose
private String city;
@SerializedName("PatientName")
@Expose
private String name;
@SerializedName("ContactNumber")
@Expose
private String contact;
@SerializedName("Time")
@Expose
private String time;
@SerializedName("DonatedBy")
@Expose
private String donar;
public PatientModel(String id, String bloodgroup, String adress, String city, String name, String contact, String time, String donar) {
this.id = id;
this.bloodgroup = bloodgroup;
this.adress = adress;
this.city = city;
this.name = name;
this.contact = contact;
this.time = time;
this.donar = donar;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBloodgroup() {
return bloodgroup;
}
public void setBloodgroup(String bloodgroup) {
this.bloodgroup = bloodgroup;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDonar() {
return donar;
}
public void setDonar(String donar) {
this.donar = donar;
}
}
SourceCode ClickHere
Comments