Yandex API 地理编码

Yandex API geocode

我正在尝试使用 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(...