Just simple log:
Assume we have the java application below:
package test;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class GeocodeService {
public static void main (String args[]) {
String result = null;
try {
System.out.println("First request\n\n");
// request service by Lat, Lng
StringBuilder sb = getResponse("http://maps.googleapis.com/maps/api/geocode/json?latlng=40.70594140,-74.0088760&sensor=true&language=ja");
result = sb.toString();
System.out.println(result+"\n");
System.out.println("Second request\n\n");
sb.setLength(0);
// request service by address
sb = getResponse("http://maps.googleapis.com/maps/api/geocode/json?address="
+java.net.URLEncoder.encode("59 ウォール街 マンハッタン ニューヨーク 10005 アメリカ合衆国", "UTF-8")+"&sensor=true&language=en");
result = sb.toString();
System.out.println(result);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static StringBuilder getResponse(String path){
try {
System.out.println(path);
java.net.URL url = new java.net.URL(path);
java.net.HttpURLConnection uc = (java.net.HttpURLConnection) url.openConnection();
uc.setRequestProperty("User-agent", "Mozilla/5.0");
uc.setRequestProperty("Accept-Charset", "UTF-8"); // encoding
uc.setReadTimeout(30000);// timeout limit
uc.connect();// connect
int status = uc.getResponseCode();
switch (status) {
case java.net.HttpURLConnection.HTTP_GATEWAY_TIMEOUT://504 timeout
break;
case java.net.HttpURLConnection.HTTP_FORBIDDEN://403 forbidden
break;
case java.net.HttpURLConnection.HTTP_INTERNAL_ERROR://500 server error
break;
case java.net.HttpURLConnection.HTTP_NOT_FOUND://404 not exist
break;
case java.net.HttpURLConnection.HTTP_OK: // ok
InputStreamReader reader = new InputStreamReader(uc.getInputStream(), "UTF-8");
int ch;
StringBuilder sb = new StringBuilder("");
while((ch = reader.read())!= -1){
sb.append((char)ch);
}
return sb;
}
} catch (java.net.MalformedURLException e) { // invalid address format
e.printStackTrace();
} catch (java.io.IOException e) { // connection broken
e.printStackTrace();
}
return null;
}
}
The result will be:
First request
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.70594140,-74.0088760&sensor=true&language=ja
{
"results" : [
{
"address_components" : [
{
"long_name" : "2",
"short_name" : "2",
"types" : [ "street_number" ]
},
{
"long_name" : "ハノーバー・ストリート",
"short_name" : "ハノーバー・ストリート",
"types" : [ "route" ]
},
{
"long_name" : "ダウンタウン",
"short_name" : "ダウンタウン",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "マンハッタン",
"short_name" : "マンハッタン",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "locality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "アメリカ合衆国",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "10005",
"short_name" : "10005",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "2 ハノーバー・ストリート マンハッタン ニューヨーク 10005 アメリカ合衆国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.70595330,
"lng" : -74.00886570
},
"southwest" : {
"lat" : 40.70549480,
"lng" : -74.00910170
}
},
"location" : {
"lat" : 40.70594550,
"lng" : -74.00888990
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 40.70707303029150,
"lng" : -74.00763471970851
},
"southwest" : {
"lat" : 40.70437506970850,
"lng" : -74.01033268029151
}
}
},
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "Wall St",
"short_name" : "Wall St",
"types" : [ "subway_station", "establishment", "transit_station" ]
},
{
"long_name" : "ダウンタウン",
"short_name" : "ダウンタウン",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "マンハッタン",
"short_name" : "マンハッタン",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "locality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "アメリカ合衆国",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "10005",
"short_name" : "10005",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Wall St, ニューヨーク 10005 アメリカ合衆国",
"geometry" : {
"location" : {
"lat" : 40.7068210,
"lng" : -74.00910
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.70816998029150,
"lng" : -74.00775101970849
},
"southwest" : {
"lat" : 40.70547201970850,
"lng" : -74.01044898029151
}
}
},
"types" : [ "subway_station", "establishment", "transit_station" ]
},
{
"address_components" : [
{
"long_name" : "10265",
"short_name" : "10265",
"types" : [ "postal_code" ]
},
{
"long_name" : "ダウンタウン",
"short_name" : "ダウンタウン",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "マンハッタン",
"short_name" : "マンハッタン",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "locality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "アメリカ合衆国",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "マンハッタン ニューヨーク 10265 アメリカ合衆国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.70839880,
"lng" : -74.00650899999999
},
"southwest" : {
"lat" : 40.70530210,
"lng" : -74.01091610
}
},
"location" : {
"lat" : 40.70556480,
"lng" : -74.0081070
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.70839880,
"lng" : -74.00650899999999
},
"southwest" : {
"lat" : 40.70530210,
"lng" : -74.01091610
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "10005",
"short_name" : "10005",
"types" : [ "postal_code" ]
},
{
"long_name" : "ウォール・ストリート",
"short_name" : "ウォール・ストリート",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "マンハッタン",
"short_name" : "マンハッタン",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "WALL STREET",
"short_name" : "WALL STREET",
"types" : [ "locality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "アメリカ合衆国",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "WALL STREET ニューヨーク 10005 アメリカ合衆国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.7091540,
"lng" : -73.99918690
},
"southwest" : {
"lat" : 40.6949950,
"lng" : -74.0135830
}
},
"location" : {
"lat" : 40.69984330,
"lng" : -74.00724360
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.7091540,
"lng" : -73.99918690
},
"southwest" : {
"lat" : 40.6949950,
"lng" : -74.0135830
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "ダウンタウン",
"short_name" : "ダウンタウン",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "マンハッタン",
"short_name" : "マンハッタン",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "locality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "アメリカ合衆国",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "ダウンタウン ニューヨーク アメリカ合衆国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.74252620,
"lng" : -73.97164459999999
},
"southwest" : {
"lat" : 40.70046310,
"lng" : -74.01933500000001
}
},
"location" : {
"lat" : 40.72300840,
"lng" : -74.00063279999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.74252620,
"lng" : -73.97164459999999
},
"southwest" : {
"lat" : 40.70046310,
"lng" : -74.01933500000001
}
}
},
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "マンハッタン",
"short_name" : "マンハッタン",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "locality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "アメリカ合衆国",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "マンハッタン ニューヨーク アメリカ合衆国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.8822140,
"lng" : -73.9070
},
"southwest" : {
"lat" : 40.67954790,
"lng" : -74.0472850
}
},
"location" : {
"lat" : 40.78343450,
"lng" : -73.96624950
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.8200450,
"lng" : -73.90331300000001
},
"southwest" : {
"lat" : 40.6980780,
"lng" : -74.03514899999999
}
}
},
"types" : [ "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "アメリカ合衆国",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "ニューヨーク アメリカ合衆国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.8822140,
"lng" : -73.9070
},
"southwest" : {
"lat" : 40.67954790,
"lng" : -74.0472850
}
},
"location" : {
"lat" : 40.78306030,
"lng" : -73.97124880
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.8822140,
"lng" : -73.9070
},
"southwest" : {
"lat" : 40.67954790,
"lng" : -74.0472850
}
}
},
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "locality", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "ニューヨーク",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "ニューヨーク",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "アメリカ合衆国",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "ニューヨーク アメリカ合衆国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.9175770,
"lng" : -73.7002720
},
"southwest" : {
"lat" : 40.4959080,
"lng" : -74.25908790
}
},
"location" : {
"lat" : 40.71435280,
"lng" : -74.00597309999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.9175770,
"lng" : -73.7002720
},
"southwest" : {
"lat" : 40.4959080,
"lng" : -74.25908790
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "ニューヨーク",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "アメリカ合衆国",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "ニューヨーク アメリカ合衆国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 45.0158650,
"lng" : -71.85626990
},
"southwest" : {
"lat" : 40.49594540,
"lng" : -79.76214390
}
},
"location" : {
"lat" : 43.29942850,
"lng" : -74.21793260000001
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 45.0158650,
"lng" : -71.85626990
},
"southwest" : {
"lat" : 40.49594540,
"lng" : -79.76214390
}
}
},
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "アメリカ合衆国",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "アメリカ合衆国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 71.3898880,
"lng" : -66.94539480000002
},
"southwest" : {
"lat" : 18.91106430,
"lng" : 172.45469670
}
},
"location" : {
"lat" : 37.090240,
"lng" : -95.7128910
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 71.3898880,
"lng" : -66.94539480000002
},
"southwest" : {
"lat" : 18.91106430,
"lng" : 172.45469670
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
Second request
http://maps.googleapis.com/maps/api/geocode/json?address=59+%E3%82%A6%E3%82%A9%E3%83%BC%E3%83%AB%E8%A1%97+%E3%83%9E%E3%83%B3%E3%83%8F%E3%83%83%E3%82%BF%E3%83%B3+%E3%83%8B%E3%83%A5%E3%83%BC%E3%83%A8%E3%83%BC%E3%82%AF+10005+%E3%82%A2%E3%83%A1%E3%83%AA%E3%82%AB%E5%90%88%E8%A1%86%E5%9B%BD&sensor=true&language=en
{
"results" : [
{
"address_components" : [
{
"long_name" : "59",
"short_name" : "59",
"types" : [ "street_number" ]
},
{
"long_name" : "Wall St",
"short_name" : "Wall St",
"types" : [ "route" ]
},
{
"long_name" : "Lower Manhattan",
"short_name" : "Lower Manhattan",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Manhattan",
"short_name" : "Manhattan",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "New York",
"short_name" : "New York",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New York",
"short_name" : "New York",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "10005",
"short_name" : "10005",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "59 Wall St, New York, NY 10005, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.70593059999999,
"lng" : -74.00882020
},
"southwest" : {
"lat" : 40.70591870,
"lng" : -74.00883060
}
},
"location" : {
"lat" : 40.70591870,
"lng" : -74.00883060
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 40.70727363029150,
"lng" : -74.00747641970850
},
"southwest" : {
"lat" : 40.70457566970850,
"lng" : -74.01017438029150
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
Download:
The file GeocodeService.java at github
https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/Net/src/test
Reference:
http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests
http://code.google.com/intl/en/apis/maps/documentation/geocoding/