0
点赞
收藏
分享

微信扫一扫

Baidu: 地址解析

绣文字 2022-07-29 阅读 63


package service;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class GetBaiduLocation {

public static String getResponse(String url) throws IOException
{


URL u=new URL(url);
InputStream in=u.openConnection().getInputStream();
byte[] b=new byte[1024];
int length=in.read(b);

String str=new String(b,0,length,"UTF-8");
System.out.println(str);
JsonObject responseObj = new JsonParser().parse(str).getAsJsonObject();
JsonObject resultObj=responseObj.getAsJsonObject("result");
String address=resultObj.get("formatted_address").getAsString();



return address;
}

public static void main(String[] args) throws IOException {
String tmp="http://api.map.baidu.com/reverse_geocoding/v3/?ak=ED67222d6b2a2a35e48874f209788ac3&output=json&coordtype=wgs84ll&location=39.9638,116.318";
System.out.println(getResponse(tmp));

}

}


举报

相关推荐

0 条评论