2

我正在尝试使用 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) {}
    }
}

线程“主”java.lang.IllegalArgumentException 中的异常:无效 uri ' https://geocode-maps.yandex.ru/1.x/?geocode=Санкт-Петербург ':无效查询

4

1 回答 1

1

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

请试试

address = URLEncoder.encode(... 
于 2016-05-05T13:32:04.890 回答