The provided Java example demonstrates how to make an HTTP GET request using HttpURLConnection to a given API endpoint for object detection, specifically targeting license plate recognition. It constructs the URL with necessary parameters, handles HTTP connections, and outputs the response content. Error handling is included for exceptions.
Java Example (using HttpURLConnection) #
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class AnprRequest {
public static void main(String[] args) {
try {
String imageUrl = "https://example.com/car.jpg";
String apiKey = "your_api_key";
String ga = "your_ga_value";
String urlString = "https://your-api-endpoint.com/ObjectDetection" +
"?Image_url=" + java.net.URLEncoder.encode(imageUrl, "UTF-8") +
"&Api_key=" + apiKey +
"&GA=" + ga +
"&Detect_license_plate=true" +
"&Read_license_plate=true";
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
System.out.println(content.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}