
What is Retrofit Http Library in Android ?
Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.
Retrofit is not to the details of Retrofit 1.x versions but It is Jump into Retrofit 2 Version directly which has a lot of new features and a changed internal API compared to the previous versions.
Retrofit 2 is a by default OKHttp networking layer and is built on top of it.
Retrofit is a automatically serialises the JSON response using a POJO(Plain Old Java Object) we must be defined in advanced for the JSON Structure.
Serialized JSON we need a convert to Gson.And then Access And Retrieve Data from Retrofit to Your App.
What is Gson in Android ?
Gson is a java library that converts Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.
How to Fetch Retrofit ListView Data in Android ?
In this Tutorial Using Retrofit Fetch the Data from url to ListView. It is fetch Images And Texts From URL. And Then Set the Adapter of ListView and Display the List of Item in Retrofit.
I have Create an Object of Retrofit and then Create Interface. Interface is used to Send and Receive Data from URL. In this Interface Method Called on your Activity or Fragment and then Pass the POJO Class to get And Set Data into your Retrofit to URL.
Example Of Retrofit with ListView in Android
Declared Permission in AndroidManifest.xml File.
1 |
<uses-permission android:name="android.permission.INTERNET" /> |
Add Dependency in App Level build.gradle File.
1 2 3 |
compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.google.code.gson:gson:2.6.2' compile 'com.squareup.retrofit2:retrofit:2.0.2' |
Create activity_main.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.bhaumik.networkingtask.MainActivity"> <ListView android:id="@+id/listview_okhttp" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> |
Create item_list.xml File Layout Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/retrofit_img_listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp"/> </android.support.v7.widget.CardView> |
Create RetrofitData.java Class Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.example.bhaumik.networkingtask; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class RetrofitData { public static final String BASE_URL = "http://www.bing.com/"; public static Retrofit retrofit = null; public static Retrofit getRetrofit(){ if(retrofit == null){ retrofit = new Retrofit.Builder().baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } } |
Create Network.java Class To POJO Class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
package com.example.bhaumik.networkingtask.pojo; import java.util.List; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class Network { @SerializedName("images") @Expose private List<Image> images = null; public List<Image> getImages() { return images; } public void setImages(List<Image> images) { this.images = images; } public class Image { @SerializedName("startdate") @Expose private String startdate; @SerializedName("fullstartdate") @Expose private String fullstartdate; @SerializedName("enddate") @Expose private String enddate; @SerializedName("url") @Expose private String url; @SerializedName("urlbase") @Expose private String urlbase; @SerializedName("copyright") @Expose private String copyright; @SerializedName("copyrightlink") @Expose private String copyrightlink; @SerializedName("quiz") @Expose private String quiz; @SerializedName("wp") @Expose private Boolean wp; @SerializedName("hsh") @Expose private String hsh; @SerializedName("drk") @Expose private Integer drk; @SerializedName("top") @Expose private Integer top; @SerializedName("bot") @Expose private Integer bot; @SerializedName("hs") @Expose private List<Object> hs = null; public String getStartdate() { return startdate; } public void setStartdate(String startdate) { this.startdate = startdate; } public String getFullstartdate() { return fullstartdate; } public void setFullstartdate(String fullstartdate) { this.fullstartdate = fullstartdate; } public String getEnddate() { return enddate; } public void setEnddate(String enddate) { this.enddate = enddate; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUrlbase() { return urlbase; } public void setUrlbase(String urlbase) { this.urlbase = urlbase; } public String getCopyright() { return copyright; } public void setCopyright(String copyright) { this.copyright = copyright; } public String getCopyrightlink() { return copyrightlink; } public void setCopyrightlink(String copyrightlink) { this.copyrightlink = copyrightlink; } public String getQuiz() { return quiz; } public void setQuiz(String quiz) { this.quiz = quiz; } public Boolean getWp() { return wp; } public void setWp(Boolean wp) { this.wp = wp; } public String getHsh() { return hsh; } public void setHsh(String hsh) { this.hsh = hsh; } public Integer getDrk() { return drk; } public void setDrk(Integer drk) { this.drk = drk; } public Integer getTop() { return top; } public void setTop(Integer top) { this.top = top; } public Integer getBot() { return bot; } public void setBot(Integer bot) { this.bot = bot; } public List<Object> getHs() { return hs; } public void setHs(List<Object> hs) { this.hs = hs; } @Override public String toString() { return "Image{" + "startdate='" + startdate + '\'' + ", fullstartdate='" + fullstartdate + '\'' + ", enddate='" + enddate + '\'' + ", url='" + url + '\'' + ", urlbase='" + urlbase + '\'' + ", copyright='" + copyright + '\'' + ", copyrightlink='" + copyrightlink + '\'' + ", quiz='" + quiz + '\'' + ", wp=" + wp + ", hsh='" + hsh + '\'' + ", drk=" + drk + ", top=" + top + ", bot=" + bot + ", hs=" + hs + '}'; } } } |
Create Interface in RetrofitApi.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.example.bhaumik.networkingtask; import com.example.bhaumik.networkingtask.pojo.Network; import retrofit2.Call; import retrofit2.http.Field; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.POST; public interface RetrofitApi { @GET("/HPImageArchive.aspx?format=js&idx=0&n=50&mkt=en-US") Call<Network> getImageData(); } |
Create MyListAdapter.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package com.example.bhaumik.networkingtask; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.LinearSmoothScroller; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.bhaumik.networkingtask.R; import com.example.bhaumik.networkingtask.pojo.Image; import java.util.List; public class MyListAdapter extends BaseAdapter{ List<Image> list; Context context; public MyListAdapter(Context context,List<Image> list) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { ViewHolder viewHolder; if (convertView == null){ View view = LayoutInflater.from(context).inflate(R.layout.item_list,parent,false); viewHolder = new ViewHolder(view); view.setTag(viewHolder); }else{ viewHolder = (ViewHolder) convertView.getTag(); } String url = "http://www.bing.com/" + list.get(position).getUrl(); Glide.with(context).load(url).into(viewHolder.imageView); return viewHolder.itemView; } public static class ViewHolder{ ImageView imageView; View itemView; public ViewHolder(View itemView) { this.itemView = itemView; imageView = itemView.findViewById(R.id.retrofit_img_listView); } } } |
Create MainActivity.java Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.example.bhaumik.networkingtask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ListView; import com.example.bhaumik.networkingtask.adapter.MyListAdapter; import com.example.bhaumik.networkingtask.interfaces.Api; import com.example.bhaumik.networkingtask.pojo.Image; import com.example.bhaumik.networkingtask.pojo.Network; import com.example.bhaumik.networkingtask; import java.util.ArrayList; import java.util.List; import retrofit2.Call; public class MainActivity extends AppCompatActivity { List<Image> list; MyListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView listView = (ListView) findViewById(R.id.listview_okhttp); list = new ArrayList<>(); Api api = RetrofitData.getRetrofit().create(Api.class); Call<Network> call = api.getImageData(); call.enqueue(new retrofit2.Callback<Network>() { @Override public void onResponse(Call<Network> call, retrofit2.Response<Network> response) { if (response.isSuccessful()){ list = response.body().getImages(); adapter = new MyListAdapter(MainActivity.this,list); listView.setAdapter(adapter); } } @Override public void onFailure(Call<Network> call, Throwable t) { Log.d("tag",t.getMessage()); } }); } } |

Above the Code To Create Simple ListView and then Create RetrofitData Class to Object of Retrofit and then Put the URL from Get the Data and and then Set The ListView Adapter Data and Display Data.
In this Tutorial used to Glide Library to Download Image from Server and then set the Image in ImageView. In this Tutorial Create Custom ListView Adapter and then set the custom Layout File to Declared ImageView and then Fetch Data from URL To Bind this Layout Adapter and then Set The Data from Layout View.