Add Library
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.android.support:recyclerview-v7:26.0.+'
RetrofitDemoActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.bhaumik.networkingtask.RetrofitDemoActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_retrofit"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
retrofit_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/retrofit_image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"/>
</LinearLayout>
RetrofitDemoActivity.java
package com.example.bhaumik.networkingtask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.example.bhaumik.networkingtask.adapter.RetrofitAdapter;
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.retrofits.ApiClient;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class RetrofitDemoActivity extends AppCompatActivity {
RecyclerView recyclerView;
RetrofitAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit_demo);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view_retrofit);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new RetrofitAdapter(RetrofitDemoActivity.this);
recyclerView.setAdapter(adapter);
Api api = ApiClient.getRetrofit().create(Api.class);
Call<Network> call = api.getImageData();
call.enqueue(new Callback<Network>() {
@Override
public void onResponse(Call<Network> call, Response<Network> response) {
List<Image> list = response.body().getImages();
adapter.addImage(list);
}
@Override
public void onFailure(Call<Network> call, Throwable t) {
Log.d("tag","Error -> " +t.toString());
}
});
}
}
RetrofitAdapter.java
package com.example.bhaumik.networkingtask.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.example.bhaumik.networkingtask.R;
import com.example.bhaumik.networkingtask.pojo.Image;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Bhaumik on 9/1/2017.
*/
public class RetrofitAdapter extends RecyclerView.Adapter<RetrofitAdapter.RetrofitHolder>{
List<Image> list;
Context context;
public RetrofitAdapter(Context context) {
list = new ArrayList<>();
this.context = context;
}
@Override
public RetrofitHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.retrofit_item,parent,false);
return new RetrofitHolder(view);
}
@Override
public void onBindViewHolder(RetrofitHolder holder, int position) {
String url = "http://www.bing.com/" + list.get(position).getUrl();
Glide.with(context).load(url).into(holder.imageView);
}
@Override
public int getItemCount() {
return list.size();
}
public static class RetrofitHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public RetrofitHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.retrofit_image_view);
}
}
public void addImage(List<Image> data){
list.addAll(data);
notifyDataSetChanged();
}
}
Create An Interface to Retrofit Api Call.
Api.java
package com.example.bhaumik.networkingtask.interfaces;
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;
/**
* Created by Bhaumik on 9/1/2017.
*/
public interface Api {
@GET("/HPImageArchive.aspx?format=js&idx=0&n=50&mkt=en-US")
Call<Network> getImageData();
}
How to Create Retrofit Object And Build Url in retrofit
package com.example.bhaumik.networkingtask.retrofits;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
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;
}
}
0 comments:
Post a Comment