Java Wrapper for NewsAPI
A Java wrapper for V2 of the NewsApi.
Insert the following Maven dependency in your pom.xml:
<dependency>
<groupId>io.github.ccincharge</groupId>
<artifactId>newsapi</artifactId>
<version>1.0</version>
</dependency>
Instantiate a new instance of the NewsApi class with your API key.
NewsApi newsApi = new NewsApi(<API_KEY>);
Build up a request with desired parameters. For a full listing of available request parameters, consult the official
NewsAPI docs:
https://newsapi.org/docs/endpoints/top-headlines
https://newsapi.org/docs/endpoints/everything
https://newsapi.org/docs/endpoints/sources
For this example, we’ll search for English articles about bitcoin.
RequestBuilder bitcoinRequest = new RequestBuilder()
.setQ("bitcoin")
.setLanguage("en");
There are numerous parameters available. Q and Language are an example of two of the parameters. For the full set of
available parameters, consult the RequestBuilder Javadoc.
The top-headlines endpoint returns a list of top headlines that match the given request parameters.
RequestBuilder bitcoinRequest = new RequestBuilder()
.setQ("bitcoin")
.setLanguage("en");
ApiArticlesResponse apiArti = newsApi.sendTopRequest(bitcoinRequest);
The everything endpoint returns a list of all articles that match the given request parameters.
It is possible that there are more articles that match given query criteria than the REST API will return in one
response.
To page through different pages of results, use the .setPage() method for the request.
RequestBuilder bitcoinRequest = new RequestBuilder()
.setQ("bitcoin")
.setLanguage("en")
.setPage(2);
ApiArticlesResponse apiArticlesResponse = newsApi.sendEverythingRequest(bitcoinRequest);
The sources endpoint returns a list of all news sources that match the given query criteria.
RequestBuilder sourcesRequest = new RequestBuilder().setLanguage("en");
ApiSourcesResponse apiSourcesResponse = newsApi.sendSourcesRequest(sourcesRequest);
The top-headlines and the everything endpoints return an ApiArticlesResponse object. To work with this object, simply
issue a method call with the name of the desired response attribute, in order to access the value associated with that
attribute. Some examples are provided below:
RequestBuilder bitcoinRequest = new RequestBuilder()
.setQ("bitcoin")
.setLanguage("en");
ApiArticlesResponse apiArticlesResponse = newsApi.sendTopRequest(bitcoinRequest);
String responseStatus = apiArticlesResponse.status();
// .articles() method returns an ArrayList of articles
ArrayList<Article> newsArticles = apiArticlesResponse.articles();
Article firstArticle = newsArticles.get(0);
String firstArticleTitle = firstArticle.title();
For the full list of available method calls, consult the Javadoc.
The sources endpoint returns an ApiSourcesResponse object. To work with this object, simply issue a method call with
the name of the desired response attribute, in order to access the value associated with that attribute. Some examples
are provided below:
RequestBuilder sourcesRequest = new RequestBuilder().setLanguage("en");
ApiSourcesResponse apiSourcesResponse = newsApi.sendSourcesRequest(sourcesRequest);
// .sources() method returns an ArrayList of sources
ArrayList<Source> newsSources = apiSourcesResponse.sources();
Source firstSource = newsSources.get(0);
String firstSourceName = firstSource.name();
Create a fork of this repo, and commit your changes to a branch of your fork. Issue a PR to this repository when done.
Before beginning any PR, see if your changes correspond to an existing issue. If not, create a new issue. It is best
to leave a comment asking to be assigned to an issue before starting your PR. Attach your PR to an issue before
tarting.
Your PR must pass all tests and must not break the CI build to be merged.