Android news api with retrofit gson picasso
Here's an example of how to use Retrofit, Gson, and Picasso to fetch and display news articles from an Android API:
Step 1: Create a Retrofit instance
First, add the Retrofit and OkHttp dependencies to your Android project's build.gradle file:
dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
}
Then, create a Retrofit instance:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://newsapi.org/v2/")
.addConverterFactory(GsonConverterFactory.create())
.build();
Step 2: Define the API interface
Create an interface that defines the API endpoints:
public interface NewsApi {
@GET("top-headlines?country=us&apiKey=YOUR_API_KEY")
Call<NewsResponse> getTopHeadlines();
}
Replace YOUR_API_KEY
with your actual API key from NewsAPI.org.
Step 3: Use Retrofit to fetch data
Use the Retrofit instance to fetch the top headlines:
NewsApi newsApi = retrofit.create(NewsApi.class);
Call<NewsResponse> call = newsApi.getTopHeadlines();
call.enqueue(new Callback<NewsResponse>() {
@Override
public void onResponse(Call<NewsResponse> call, Response<NewsResponse> response) {
NewsResponse newsResponse = response.body();
// Process the news response
}
@Override
public void onFailure(Call<NewsResponse> call, Throwable t) {
// Handle failure
}
});
Step 4: Parse the JSON response using Gson
Use Gson to parse the JSON response:
public class NewsResponse {
private List<Article> articles;
public List<Article> getArticles() {
return articles;
}
}
public class Article {
private String title;
private String description;
private String url;
private String urlToImage;
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getUrl() {
return url;
}
public String getUrlToImage() {
return urlToImage;
}
}
Step 5: Display the news articles using Picasso
Use Picasso to display the news article images:
public class NewsFragment extends Fragment {
private RecyclerView recyclerView;
private NewsAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news, container, false);
recyclerView = view.findViewById(R.id.recycler_view);
adapter = new NewsAdapter();
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NewsApi newsApi = retrofit.create(NewsApi.class);
Call<NewsResponse> call = newsApi.getTopHeadlines();
call.enqueue(new Callback<NewsResponse>() {
@Override
public void onResponse(Call<NewsResponse> call, Response<NewsResponse> response) {
NewsResponse newsResponse = response.body();
adapter.setArticles(newsResponse.getArticles());
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<NewsResponse> call, Throwable t) {
// Handle failure
}
});
}
}
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private List<Article> articles;
public NewsAdapter() {
articles = new ArrayList<>();
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Article article = articles.get(position);
holder.titleTextView.setText(article.getTitle());
holder.descriptionTextView.setText(article.getDescription());
Picasso.get().load(article.getUrlToImage()).into(holder.imageView);
}
@Override
public int getItemCount() {
return articles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public TextView descriptionTextView;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.title_text_view);
descriptionTextView = itemView.findViewById(R.id.description_text_view);
imageView = itemView.findViewById(R.id.image_view);
}
}
}
In this example, we create a NewsFragment
that fetches the top headlines using Retrofit and displays them in a RecyclerView using Picasso. The NewsAdapter
class is responsible for binding the data to the RecyclerView items.
Note that you'll need to replace YOUR_API_KEY
with your actual API key from NewsAPI.org.