gpt4 book ai didi

java - Yandex API 地理编码

转载 作者:行者123 更新时间:2023-11-30 10:45:21 27 4
gpt4 key购买 nike

我正在尝试使用 Yandex 地理编码接收地址坐标。为了开始,我直接创建了一个查询,就像这里描述的那样 https://tech.yandex.ru/maps/doc/geocoder/desc/concepts/input_params-docpage/

但是,它响应说 uri 不正确。正如我发现的那样,这是因为 uri 包含俄语字母。我尝试使用 URLEncoder 修复它,但没有任何改变。将不胜感激任何形式的帮助。

import com.sun.deploy.net.URLEncoder;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

public class Main {
public static void main(String[] args) {
String address = "Санкт-Петербург";
try {
URLEncoder.encode(address, "UTF-8");
} catch (Exception e) {
System.out.print("BAD");
}
System.out.println(address);
HttpClient client = new HttpClient();
String request = "https://geocode-maps.yandex.ru/1.x/?geocode=" + address;
GetMethod method = new GetMethod(request);
String Lat="",Long="";
try {
client.executeMethod(method);
String s = method.getResponseBodyAsString();
System.out.print(s);
} catch (Exception e) {}
}
}

Exception in thread "main" java.lang.IllegalArgumentException: Invalid uri 'https://geocode-maps.yandex.ru/1.x/?geocode=Санкт-Петербург': Invalid query

最佳答案

您实际上并未对字符串进行编码。

请试试

address = URLEncoder.encode(... 

关于java - Yandex API 地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37050710/

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