Skip to main content

Retrofit Android-Fetching Data With Retrofit-teachmeandroidhub


Teachmeandroidhub

TeachMeAndroidhub


            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

Manpreet kaur said…
This comment has been removed by the author.
Sandeep said…
it is awesome sir this code is working for me

Popular posts from this blog

Android Tutorial:-How to create View Pager With Cover-Flow-Teachmeandroidhub(An animation Under Viewpager).

View-Pager  with Cover-flow  :- This Android tutorial  is all about how to create a wonderful  layout using-view pager  with some special  effect  that called carousel effect also to do such  layout  you don’t need  to add  any  dependencies  to  the  gradle  so let’s start this  tutorial first of all just need to create new Project in android studio I hope you know about how to create new project in android studio   Step -1:- In  this step you have create main activity that may be created with your  new project  creation this layout contain view pager that will adapt image view that I am going to show in this tutorial Activity_main.xml <? xml version= "1.0" encoding= "utf-8" ?> < LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"     xmlns: tools = "http://schemas.android.com/tools"     android :id= "@+id/activity_main_layout"     android :layout_width= "match_pa

retrofit android-Post Data Using Retrofit in Android -Teachmeandroidhub

Teachmeandroidhub Retrofit android-Post Data Using Retrofit in Android: Let's start work with Retrofit in android retrofit is most latest library that work with Rest Api. it is fast and easiest way to posting and getting data from server . Basically retrofit works on okhttp and we use retrofit android for sending data to json on internet so  in this topic I'll explain how to post data using Retrofit Step by step that how to work  Retrofit in android . Step -1. Create a new Project name it as you want Step -2. Add Dependencies in app level gradle like below     compile 'com.squareup.retrofit2:retrofit:2.1.0'     compile 'com.google.code.gson:gson:2.6.2'     compile 'com.squareup.retrofit2:converter-gson:2.1.0' Step -3  Design your UI with XML Like  activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Random Chat Privacy Policy

                                                                           Random Chat Privacy Policy Introduction: Random chat  is an android application that enable you to connect with anyone in the world anonymously and you don't need to login with your credential.  **Privacy Policy** Decode Technologies built the Random Chat app as a Free app. This SERVICE is provided by Decode Technologies at no cost and is intended for use as is. This page is used to inform visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service. If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect is used for providing and improving the Service. I will not use or share your information with anyone except as described in this Privacy Policy. The terms used in this Privacy Policy have the same meanings as in our Terms and Conditions,