Tutorial: How to Use an IP Location Finding API with Java

To use an IP location finding API, you need to send a GET request to the API's endpoint. Here's how you can do it using Java:

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

import org.json.JSONArray;
import org.json.JSONObject;

public class Main {
    public static void main(String[] args) throws Exception {
        String ip = "IP_Address"; // Replace with the IP Address you want to lookup
        String token = "YOUR_API_KEY"; // Replace with your actual API key
        String url = "https://api.findip.net/" + ip + "/?token=" + token;
        
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        
        JSONObject data = new JSONObject(response.toString());

        System.out.println("City Name: " + data.getJSONObject("city").getJSONObject("names").getString("en"));
        System.out.println("Continent Code: " + data.getJSONObject("continent").getString("code"));
        System.out.println("Country Name: " + data.getJSONObject("country").getJSONObject("names").getString("en"));
        System.out.println("Latitude: " + data.getJSONObject("location").getDouble("latitude"));
        System.out.println("Longitude: " + data.getJSONObject("location").getDouble("longitude"));
        System.out.println("Time Zone: " + data.getJSONObject("location").getString("time_zone"));
        System.out.println("Weather Code: " + data.getJSONObject("location").getString("weather_code"));
        
        JSONArray subdivisions = data.getJSONArray("subdivisions");
        for (int i = 0; i < subdivisions.length(); i++) {
            JSONObject subdivision = subdivisions.getJSONObject(i);
            if (subdivision.getJSONObject("names").has("en")) {
                System.out.println("Subdivision Name: " + subdivision.getJSONObject("names").getString("en"));
            }
        }
        
        System.out.println("Autonomous System Number: " + data.getJSONObject("traits").getInt("autonomous_system_number"));
        System.out.println("Autonomous System Organization: " + data.getJSONObject("traits").getString("autonomous_system_organization"));
        System.out.println("Connection Type: " + data.getJSONObject("traits").getString("connection_type"));
        System.out.println("ISP: " + data.getJSONObject("traits").getString("isp"));
        System.out.println("User Type: " + data.getJSONObject("traits").getString("user_type"));
    }
}

                    

Remember to replace 'IP_Address' and 'YOUR_API_KEY' with the actual IP address you want to verify and your actual API key. Also, ensure to add the org.json package to your project.