0
我写了下面的代码来获得一个地址使用谷歌在服务端地图地理编码服务
package test;
进口java.net.HttpURLConnection中的坐标; import java.net.URL; import sun.net.www.content.text.PlainTextInputStream;
公共类A {
public static void main(String[] arg) throws Exception{
String address = "台北市信義路五段七號101樓";
// 查詢經緯度
String output = "csv";
String key = "";
String url = "http://maps.google.com/maps/geo?q=台北市信義路五段七號101樓&output=csv&key=ABQIAAAAXDq__hWKi9eMCwnn7LrMCxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSnSVp_Xlsd4Ph5iyMua7PE5E0x_A";
URL iurl = new URL(url);
HttpURLConnection uc = (HttpURLConnection)iurl.openConnection();
uc.connect();
Object content = uc.getContent();
// 讀取結果
PlainTextInputStream sr = (PlainTextInputStream)content;
byte[] buf = new byte[2000];
// 解析 200,8,25.033408,121.564099 (HTTP status code, accuracy, latitude, longitude)
sr.read(buf);
String[] tmpArray = new String(buf, "UTF-8").split(",");
String latitude = tmpArray[2];
String longitude = tmpArray[3];
}
}
的问题是,我得到的结果 400中的内容,我把网址在浏览器中,它返回一个200来代替。
有没有办法在没有浏览器的情况下做到这一点?