Hello friends,
I have created a very Basic News App. which fetch data from API.
further details as follow :
https://newsapi.org/If You want to create your app like i have created then you must find news API from above link and generate a Key.
After Getting your key You must have to place key in Constants.java file.
static final String BaseUrl ="https://newsapi.org/v2/top-headlines?country=in&apiKey=";
static final String API_KEY="Your Api key goes here";
In this project there is one file where volley Library is used.
file Path : PopNewsApp/app/src/main/java/com/rajkamani/popnews/apicall/ApiCall.java
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("articles");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject ArticleNews = jsonArray.getJSONObject(i);
Article article = new Article();
String author = ArticleNews.getString("author");
String title = ArticleNews.getString("title");
String des = ArticleNews.getString("description");
String articleUrl = ArticleNews.getString("url");
String imageUrl = ArticleNews.getString("urlToImage");
String time = ArticleNews.getString("publishedAt");
Source source = new Source();
JSONObject jsonObjectSource = ArticleNews.getJSONObject("source");
String publishername = jsonObjectSource.getString("name");
article.setAuthor(author);
article.setTitle(title);
article.setDescription(des);
article.setUrlToArticle(articleUrl);
article.setUrlToImage(imageUrl);
article.setPublishedAt(time);
articlesList.add(article);
source.setName(publishername);
sourcesList.add(source);
RecycleAdapter recycleAdapter = new RecycleAdapter(context, articlesList, sourcesList);
recyclerView.setAdapter(recycleAdapter);
spinKitView.setVisibility(View.INVISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, "Error In respons", Toast.LENGTH_SHORT).show();
}
}
For display news I have Used Cardview With RecyclerView and you can find recyclerview Adapter here : RecycleAdapter.java file under Adapter folder.
For display multiple category i have used TabLayout and fragments You can find all 7 Fragments in Fragment folder.
And Adapter for fragments with tablayout in Adapter Folder PagerAdapter.java