Java Reference
In-Depth Information
.
openConnection
();
try
try
(
BufferedReader is
=
new
new
BufferedReader
(
new
new
InputStreamReader
(
conn
.
getInputStream
()))) {
String line
;
while
while
((
line
=
is
.
readLine
()) !=
null
null
) {
System
.
out
.
println
(
line
);
}
}
}
}
The result comes back in the requested format. For a JSON request, we get a single long line
with all the information, for the given (O'Reilly Media) web server. Here the line has been
broken at commas to make it fit on the page:
{"ip":"207.152.124.48","country_code":"US","country_name":"United States",
"region_code":"CO","region_name":"Colorado","city":"Englewood",
"zipcode":"80111","latitude":39.6237,"longitude":-104.8738,
"metro_code":"751","areacode":"303"}
Like any commercially scaled organization, O'Reilly uses a distributed Content Distribution
Network (CDN), so the answer you get will change over time; here is a different output ob-
tained by running the same program a few minutes later:
{"ip":"69.31.106.26","country_code":"US","country_name":"United States",
"region_code":"MA","region_name":"Massachusetts","city":"Cambridge",
"zipcode":"02142","latitude":42.3626,"longitude":-71.0843,
"metro_code":"506","areacode":"617"}
You can find more information on REST services (including implementing the server-side
components for them) in Bill Burke's
RESTful Java with JAX-RS 2.0, 2nd Edition
(O'Reilly).