Java Example
This example demonstrates how to use the WebClassifAI API with Java using the HttpClient
from Java 11+.
Dependencies
Add the following dependencies to your pom.xml
if you’re using Maven:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
</dependencies>
Or to your build.gradle
if you’re using Gradle:
dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
}
Basic Example
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class WebClassifAIExample {
private static final String API_ENDPOINT = "https://api.classifai.com/v1";
private static final ObjectMapper objectMapper = new ObjectMapper();
public static Map<String, Object> classifyUrls(List<String> urls, String apiKey, String taxonomy) throws Exception {
// Create request payload
Map<String, Object> payload = Map.of(
"urls", urls,
"api_key", apiKey,
"taxonomy", taxonomy
);
// Convert payload to JSON
String jsonPayload = objectMapper.writeValueAsString(payload);
// Create HTTP client
HttpClient client = HttpClient.newHttpClient();
// Create HTTP request
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_ENDPOINT))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
// Send request and get response
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// Parse response
return objectMapper.readValue(response.body(), Map.class);
}
public static void main(String[] args) {
try {
String apiKey = "your_api_key_here";
List<String> urls = List.of(
"https://www.example.com/",
"https://www.tech-blog.com/"
);
Map<String, Object> results = classifyUrls(urls, apiKey, "iab-1.0");
// Process results
List<Map<String, Object>> resultsList = (List<Map<String, Object>>) results.get("results");
for (Map<String, Object> result : resultsList) {
String url = (String) result.get("url");
boolean scrapedSuccessfully = (boolean) result.get("scraped_successfully");
System.out.println("\nURL: " + url);
System.out.println("Scraped successfully: " + scrapedSuccessfully);
List<Map<String, Object>> predictions = (List<Map<String, Object>>) result.get("predictions");
for (Map<String, Object> prediction : predictions) {
System.out.println("\nPrediction:");
if (prediction.containsKey("tier1")) {
Map<String, Object> tier1 = (Map<String, Object>) prediction.get("tier1");
System.out.printf("Tier 1: %s (%s) - Confidence: %.2f%n",
tier1.get("name"),
tier1.get("code"),
tier1.get("confidence"));
}
if (prediction.containsKey("tier2")) {
Map<String, Object> tier2 = (Map<String, Object>) prediction.get("tier2");
System.out.printf("Tier 2: %s (%s) - Confidence: %.2f%n",
tier2.get("name"),
tier2.get("code"),
tier2.get("confidence"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Error Handling
For production use, you should implement proper error handling:
public static Map<String, Object> classifyUrls(List<String> urls, String apiKey, String taxonomy) throws Exception {
try {
// ... existing code ...
} catch (HttpTimeoutException e) {
throw new RuntimeException("Request timed out", e);
} catch (IOException e) {
throw new RuntimeException("Network error", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Request interrupted", e);
}
}
Batch Processing
For optimal performance, send multiple URLs in a single request:
// Process up to 500 URLs in a single request
List<String> urls = Collections.nCopies(500, "https://example.com");
Map<String, Object> results = classifyUrls(urls, apiKey, "iab-1.0");
Async Example
Important: We strongly recommend batching URLs in a single request (up to the 500 URL limit) for optimal performance. Only use async execution when your limits exceed 500 URLs or your architecture specifically requires it.
public static CompletableFuture<Map<String, Object>> classifyUrlsAsync(
List<String> urls, String apiKey, String taxonomy) {
HttpClient client = HttpClient.newBuilder()
.executor(Executors.newFixedThreadPool(10))
.build();
Map<String, Object> payload = Map.of(
"urls", urls,
"api_key", apiKey,
"taxonomy", taxonomy
);
try {
String jsonPayload = objectMapper.writeValueAsString(payload);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_ENDPOINT))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(responseBody -> {
try {
return objectMapper.readValue(responseBody, Map.class);
} catch (Exception e) {
throw new CompletionException(e);
}
});
} catch (Exception e) {
return CompletableFuture.failedFuture(e);
}
}
// Example usage
public static void main(String[] args) {
String apiKey = "your_api_key_here";
// For best performance, process URLs in batches of up to 500
// Only split into multiple async requests if absolutely necessary
int batchSize = 500;
List<String> allUrls = Collections.nCopies(1000, "https://www.example.com/");
List<List<String>> batches = new ArrayList<>();
for (int i = 0; i < allUrls.size(); i += batchSize) {
batches.add(allUrls.subList(i, Math.min(i + batchSize, allUrls.size())));
}
List<CompletableFuture<Map<String, Object>>> futures = new ArrayList<>();
for (List<String> batch : batches) {
futures.add(classifyUrlsAsync(batch, apiKey, "iab-1.0"));
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenAccept(v -> {
for (int i = 0; i < futures.size(); i++) {
try {
Map<String, Object> result = futures.get(i).get();
System.out.println("Processed batch " + (i+1) + " with " +
((List)result.get("results")).size() + " URLs");
} catch (Exception e) {
System.err.println("Error processing batch " + (i+1) + ": " + e.getMessage());
}
}
});
}
Additional Resources
Last updated on