A Java library to query IP addresses using the ipquery.io API.
- Query details for a specific IP address.
- Bulk query multiple IP addresses.
- Fetch your own public IP address.
To use this library, clone the repository and build it using Gradle.
- JDK 8 or later
- Gradle (if not installed, use the Gradle wrapper)
git clone https://github.com/rezwanahmedsami/ipapi-java.git
cd ipapi-java
To build the project and install dependencies, use the following Gradle command:
./gradlew build
This will download the required dependencies, compile the source code, and generate a JAR file in the build/libs/
directory.
The queryIp
method retrieves information about a specific IP address, including its ISP, location, and risk data.
import io.ipapi.IPInfo;
import io.ipapi.Ipapi;
public class Main {
public static void main(String[] args) {
Ipapi ipapi = new Ipapi("YOUR_API_KEY"); // Replace with your API key
IPInfo ipInfo = ipapi.queryIp("8.8.8.8");
if (ipInfo != null) {
System.out.println(ipInfo);
} else {
System.out.println("Failed to fetch IP information.");
}
}
}
IPInfo {
ip: "8.8.8.8",
isp: { asn: "AS15169", org: "Google LLC", isp: "Google LLC" },
location: {
country: "United States",
country_code: "US",
city: "Mountain View",
state: "California",
zipcode: "94043",
latitude: 37.436,
longitude: -122.0938,
timezone: "America/Los_Angeles",
localtime: "2024-12-11T18:26:48"
},
risk: {
is_mobile: false,
is_vpn: false,
is_tor: false,
is_proxy: false,
is_datacenter: true,
risk_score: 0
}
}
The queryBulk
method allows you to query information for multiple IP addresses at once.
import io.ipapi.Ipapi;
public class Main {
public static void main(String[] args) {
Ipapi ipapi = new Ipapi("YOUR_API_KEY"); // Replace with your API key
List<String> ips = Arrays.asList("8.8.8.8", "1.1.1.1");
List<IPInfo> ipInfos = ipapi.queryBulk(ips);
for (IPInfo info : ipInfos) {
System.out.println(info);
}
}
}
IPInfo {
ip: "8.8.8.8",
...
}
IPInfo {
ip: "1.1.1.1",
...
}
The queryOwnIp
method retrieves the public IP address of the current machine.
import io.ipapi.Ipapi;
public class Main {
public static void main(String[] args) {
Ipapi ipapi = new Ipapi("YOUR_API_KEY"); // Replace with your API key
String ip = ipapi.queryOwnIp();
if (ip != null) {
System.out.println("Your IP Address: " + ip);
} else {
System.out.println("Failed to fetch public IP address.");
}
}
}
Your IP Address: 203.0.113.45
public IPInfo queryIp(String ip);
Fetches detailed information about a specific IP address, including its ISP, location, and risk information.
ip
: AString
representing the IP address to query.
- An
IPInfo
object containing details about the IP address on success. null
if the network request fails.
public List<IPInfo> queryBulk(List<String> ips);
Fetches information for multiple IP addresses at once. Useful for batch processing.
ips
: AList<String>
containing the list of IP addresses to query.
- A
List<IPInfo>
containing details for each IP address.
public String queryOwnIp();
Fetches the public IP address of the current machine.
- A
String
containing the public IP address on success. null
if the network request fails.
To run tests for this library, use Gradle's test task:
# Run tests
./gradlew test
Gradle will run the tests and show the results in the terminal. You can also view the test results in the build/reports/tests/test/index.html
file.
Contributions are welcome! Feel free to open issues or submit pull requests on the GitHub repository.