KwabenBerko/News-API-Java


An wrapper for newsapi.org

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.kwabenberko:news-api-java:1.0.2'
	}
	dependencies {
		implementation("com.github.kwabenberko:news-api-java:1.0.2")
	}
	<dependency>
	    <groupId>com.github.kwabenberko</groupId>
	    <artifactId>news-api-java</artifactId>
	    <version>1.0.2</version>
	</dependency>

                            
    libraryDependencies += "com.github.kwabenberko" % "news-api-java" % "1.0.2"
        
        

                            
    :dependencies [[com.github.kwabenberko/news-api-java "1.0.2"]]
        
        

Readme


News-API-Java

Create an account at newsapi.org to get your API key.

Download

Using Gradle

Step 1. Add the JitPack repository to your root build.gradle file.

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Step 2 : Download via Gradle:

implementation 'com.github.KwabenBerko:News-API-Java:1.0.2'

Using Maven

Step 1. Add the JitPack repository to your pom.xml file.

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

Step 2 : Add the dependency to the dependencies in your pom.xml file:

<dependencies>
  ...
  ...
  <dependency>
    <groupId>com.github.KwabenBerko</groupId>
    <artifactId>News-API-Java</artifactId>
    <version>1.0.2</version>
  </dependency>
</dependencies>

Usage

Instantiate the NewsApiClient class:

NewsApiClient newsApiClient = new NewsApiClient("YOUR_API_KEY");

Get Top Headlines

newsApiClient.getTopHeadlines(
                new TopHeadlinesRequest.Builder()
                        .q("bitcoin")
                        .language("en")
                        .build(),
                new NewsApiClient.ArticlesResponseCallback() {
                    @Override
                    public void onSuccess(ArticleResponse response) {
                        System.out.println(response.getArticles().get(0).getTitle());
                    }

                    @Override
                    public void onFailure(Throwable throwable) {
                       System.out.println(throwable.getMessage());
                    }
                }
        );

Get Everything

newsApiClient.getEverything(
                new EverythingRequest.Builder()
                        .q("trump")
                        .build(),
                new NewsApiClient.ArticlesResponseCallback() {
                    @Override
                    public void onSuccess(ArticleResponse response) {
                        System.out.println(response.getArticles().get(0).getTitle());
                    }

                    @Override
                    public void onFailure(Throwable throwable) {
                        System.out.println(throwable.getMessage());
                    }
                }
        );

Get Sources

newsApiClient.getSources(
                new SourcesRequest.Builder()
                        .language("en")
                        .country("us")
                        .build(),
                new NewsApiClient.SourcesCallback() {
                    @Override
                    public void onSuccess(SourcesResponse response) {
                        System.out.println(response.getSources().get(0).getName());
                    }

                    @Override
                    public void onFailure(Throwable throwable) {
                        System.out.println(throwable.getMessage());
                    }
                }
        );