A simple way to access the webhose.io API from your Java code
// Create a WebhoseIOClient instance
WebhoseIOClient webhoseClient = WebhoseIOClient.getInstance(API_KEY);
// Create set of queries
Map<String, String> queries = new HashMap<String, String>();
queries.put("q", "github");
// Fetch query result
JsonElement result = webhoseClient.query("filterWebContent", queries);
System.out.println(result.getAsJsonObject().get("totalResults")); // Print posts count
To make use of the webhose.io API, you need to obtain a token that would be used on every request. To obtain an API key, create an account at https://webhose.io/auth/signup, and then go into https://webhose.io/dashboard to see your token.
To get started, you need to import the library, and set your access token. (Replace YOUR_API_KEY with your actual API key).
import com.webhoseio.sdk.WebhoseIOClient;
WebhoseIOClient webhoseClient = WebhoseIOClient.getInstance(API_KEY);
Now you can make a request and inspect the results:
// Create set of queries
Map<String, String> queries = new HashMap<String, String>();
queries.put("q", "foobar");
JsonElement result = webhoseClient.query("filterWebContent", queries);
System.out.println(result.getAsJsonObject().get("totalResults")); // Print posts count
JsonArray postArray = result.getAsJsonObject().getAsJsonArray("posts");
for(JsonElement o : postArray) {
System.out.println(o.getAsJsonObject().get("title")); // Print title
System.out.println(o.getAsJsonObject().get("author")); // Print author
System.out.println(o.getAsJsonObject().get("language")); // Print language
}
API Endpoints
The first parameter the Query
function accepts is the API endpoint string. Available endpoints:
filterWebContent
- access to the news/blogs/forums/reviews APIproductFilter
- access to data about eCommerce products/servicesdarkFilter
- access to the dark web (coming soon)
getInstance(token) Initialize WebhoseIOClient and return an instance.
WebhoseIOClient webhoseClient = WebhoseIOClient.getInstance(TOKEN);
token
- your API key
query(endpoint, queries) Convenient method to query a specific keyword. Returns JSONObject instance.
// Create set of queries
Map<String, String> queries = new HashMap<String, String>();
queries.put("q", "github"); // Query using keyword "github"
queries.put("size", "3"); // (OPTIONAL) Limit response to maximum of 3 items
// Fetch query result
JsonElement result = webhoseClient.query("filterWebContent", queries);
System.out.println(result.getAsJsonObject().get("totalResults")); // Print posts count
JsonArray postArray = result.getAsJsonObject().getAsJsonArray("posts");
for(JsonElement o : postArray) {
System.out.println(o.getAsJsonObject().get("title")); // Print title
System.out.println(o.getAsJsonObject().get("author")); // Print author
System.out.println(o.getAsJsonObject().get("language")); // Print language
}
- queries - a set of key value hashmap for each parameter to be passed to the API endpoint.
- endpoint
filterWebContent
- access to the news/blogs/forums/reviews APIproductFilter
- access to data about eCommerce products/servicesdarkFilter
- access to the dark web (coming soon)
getNext() Convenient method to fetch next query page. Returns JSONObject instance.
JsonElement result = webhoseClient.getNext();
System.out.println(result.getAsJsonObject().get("totalResults")); // Print posts count
JsonArray postArray = result.getAsJsonObject().getAsJsonArray("posts");
for(JsonElement o : postArray) {
System.out.println(o.getAsJsonObject().get("title")); // Print title
System.out.println(o.getAsJsonObject().get("author")); // Print author
System.out.println(o.getAsJsonObject().get("language")); // Print language
}