Android news app with retrofit and gson
Here is a basic example of an Android news app that uses Retrofit and Gson to fetch and display news articles:
Step 1: Create a new Android project
Create a new Android project in Android Studio and name it "NewsApp".
Step 2: Add Retrofit and Gson dependencies
In your project's build.gradle
file, add the following dependencies:
dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.google.code.gson:gson:2.8.6'
}
Step 3: Create a News API interface
Create a new Java interface NewsApi
that defines the API endpoints for fetching news articles:
public interface NewsApi {
@GET("top-headlines")
Call<NewsResponse> getTopHeadlines(@Query("country") String country, @Query("apiKey") String apiKey);
@GET("everything")
Call<NewsResponse> getEverything(@Query("q") String query, @Query("apiKey") String apiKey);
}
Step 4: Create a NewsResponse class
Create a new Java class NewsResponse
that represents the response from the API:
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: Create a NewsAdapter class
Create a new Java class NewsAdapter
that adapts the NewsResponse
object to a list of Article
objects:
public class NewsAdapter {
private List<Article> articles;
public NewsAdapter(List<Article> articles) {
this.articles = articles;
}
public List<Article> getArticles() {
return articles;
}
}
Step 6: Create a NewsFragment class
Create a new Java class NewsFragment
that fetches the news articles using Retrofit and displays them in a RecyclerView:
public class NewsFragment extends Fragment {
private NewsApi newsApi;
private RecyclerView recyclerView;
private NewsAdapter newsAdapter;
@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);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
newsApi = RetrofitClient.getInstance().create(NewsApi.class);
fetchNews();
return view;
}
private void fetchNews() {
Call<NewsResponse> call = newsApi.getTopHeadlines("us", "YOUR_API_KEY");
call.enqueue(new Callback<NewsResponse>() {
@Override
public void onResponse(Call<NewsResponse> call, Response<NewsResponse> response) {
NewsResponse newsResponse = response.body();
List<Article> articles = newsResponse.getArticles();
newsAdapter = new NewsAdapter(articles);
recyclerView.setAdapter(newsAdapter);
}
@Override
public void onFailure(Call<NewsResponse> call, Throwable t) {
Log.e("Error", t.getMessage());
}
});
}
}
Step 7: Create a RetrofitClient class
Create a new Java class RetrofitClient
that creates a Retrofit instance and provides a method to create a NewsApi instance:
public class RetrofitClient {
private static Retrofit retrofit;
public static Retrofit getInstance() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl("https://newsapi.org/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
public static NewsApi createNewsApi() {
return getInstance().create(NewsApi.class);
}
}
Step 8: Add the API key
Replace YOUR_API_KEY
with your actual API key from the News API.
Step 9: Run the app
Run the app on an emulator or a physical device to see the news articles displayed in the RecyclerView.
This is a basic example of how to use Retrofit and Gson to fetch and display news articles in an Android app. You can customize the app to fit your specific needs and add more features as needed.