gpt4 book ai didi

java - 在java中使用地理编码(GoogleMapAPI)进行地址验证

转载 作者:行者123 更新时间:2023-12-01 04:35:40 25 4
gpt4 key购买 nike

URL url = new URL("http://maps.google.com/maps/api/geocode/json?address=1600%20Amphitheatre%20Parkway&sensor=false&client_id=my_client_id&key=my_key");
URLConnection urlConnection = url.openConnection();

HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");

InputStream in = httpURLConnection.getInputStream();

它给出的连接拒绝异常。

最佳答案

您忘记实际连接:

URL url = new URL("yoururl"); 
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");

try {
// open the connection and get results as InputStream.
httpURLConnection.connect();
InputStream in = httpURLConnection.getInputStream();;

// do more things
} finally {
httpURLConnection.disconnect();
}

您还可以修改 URL 编码方式以避免错误:

private String REQUEST_URL = "http://maps.google.com/maps/api/geocode/json";
private String address = "1600 Amphitheatre Parkway";

URL url = new URL(REQUEST_URL + "?address=" + URLEncoder.encode(address, "UTF-8") + "&sensor=false&client_id=my_client_id&key=my_key"););

关于java - 在java中使用地理编码(GoogleMapAPI)进行地址验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17403034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com