This commit is contained in:
oneao committed 2025-07-31 17:32:19 +08:00
1 parent ba4506baf7
commit 5ef02d648a
6 files changed
+105 -5

No files matched your search

@@ -0,0 +1,40 @@
package com.crane.test;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class Test2 {
public static void main(String[] args) {
String lon = "116.37304";
String lat = "39.92594";
String tk = "f1dbb4a15e82006fca57d3286bbc410a"; // 替换为你的真实 key
String postStrJson = String.format("{\"lon\":%s,\"lat\":%s,\"ver\":1}", lon, lat);
String encodedPostStr = URLEncoder.encode(postStrJson, StandardCharsets.UTF_8);
String url = String.format(
"https://api.tianditu.gov.cn/geocoder?postStr=%s&type=geocode&tk=%s",
encodedPostStr, tk
);
System.out.println(url);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36")
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
String result = response.body().string();
System.out.println("响应内容:\n" + result);
} else {
System.out.println("请求失败,状态码:" + response.code());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}