News API
News API
is a REST API for searching and retrieving news articles from multiple sources on the Web. Although the 'Business'
and 'Advanced' versions aren't free, the 'Developer' is, and is
quite functional.
All that's needed to begin is an API Key which can be
obtained from the News API site, which also offers several libraries
for accessing the API in different languages. The recommended one
for Java is a bit out-of-date, and it's much easier to utilize Java
11's HttpClient class and
Google GSON to map the
retrieved JSON data into Java objects. All of this is encoded in
my NewsUtils.java and
News.java classes.
Downloads and Links
- GetSources.java:
lists all the news sources for obtaining headlines from
a given country.
- GetHeadlines.java:
gets the headlines for a specified country. If a number
is included on the command line then that article's URL
is loaded into a browser.
- GetBBC.java:
gets the headlines from
BBC News. If a number
is included on the command line then that article's URL
is loaded into a browser.
- GetTopic.java:
searches for English language articles on the topic
supplied on the command line. If a number
is included then that article's URL
is loaded into a browser.
- NewsUtils.java:
functions for using the News API, including the API key
which you'll have to edit.
- News.java:
many simple classes representing the
JSON responses by News API's
'everything', 'top-headlines', and 'sources' queries.
These classes are used by
Google GSON
to convert those responses into Java objects.
- Batch files to easily compile and run my
code: compile.bat and
run.bat
- A batch file called
curlNews.bat
that uses a Windows version of
curl to query the
API. You'll have to modify the code to use your own
API key, and the best way to view results is to
pipe it through jq.
For information about jq on Windows, see
here.
- All of these examples and support classes in one
handy zip file (8 KB).
- My copy of the Google GSON library
v.2.6.2 (204 KB). Unzip this and place the libs folder in the
same directory as the examples and batch files.
- The News API documentation.
Dr. Andrew Davison
E-mail: ad@coe.psu.ac.th
Back to the third-party libraries page