Java

Simple example to retrieve information about an airframe, by registration and display some of the resulting data.

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class FetchJsonExample
{

    public static String fetchJsonFromUrl(String urlString) throws IOException
    {
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        try
        {
            // Set up the connection
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);

            // Check if the request was successful (status code 200)
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)
            {
                // Read the response
                try
                    (BufferedReader reader = new BufferedReader(
                                                 new InputStreamReader(connection.getInputStream())))
                {
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null)
                    {
                        response.append(line);
                    }

                    return response.toString();
                }
            }
            else
            {
                throw new IOException("Error: Unable to fetch data. Status code: " + connection.getResponseCode());
            }
        }
        finally
        {
            connection.disconnect();
        }
    }

    public static void main(String[] args)
    {
        // Specify the URL you want to call and your API credentials - the test credentials always return the same item
        String API_KEY ="TESTAPIKEY"; //Replace this with your own API KEY
        String API_PASSWORD ="TESTAPIKEYPASSWORD"; //Replace this with your own API PASSWORD
        String API_ACCESS_TOKEN ="TESTAPIACCESSTOKEN"; //Replace this with your own API ACCESS TOKEN
        string API_BASE_URL="https://www.avdelphi.com/api/1.0/";
        string API_ENDPOINT="airframes";
        string API_COMMAND="info";
        string REGISTRATION="HB-JNB";
        string ITEMLIST="type_short;type_long;manufacturer";

        string targetUrl = API_BASE_URL + API_ENDPOINT +".svc?api_key=" + API_KEY + "&api_access_token=" + API_ACCESS_TOKEN  + "&cmd=" + API_COMMAND + "&registration=" + REGISTRATION + "&itemlist=" + ITEMLIST;

        try
        {
            // Call the function with the specified URL
            String jsonResult = fetchJsonFromUrl(targetUrl);

            // Display the result
            System.out.println("Result JSON:");
            System.out.println(jsonResult);
        }
        catch (IOException e)
        {
            // Handle any exceptions that might occur during the request
            e.printStackTrace();
        }
    }
}

Copy to clipboard

Sample output