A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 专注的一批 中级黑马   /  2019-12-13 14:43  /  1742 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


import com.alibaba.fastjson.JSONObject;
import com.cxt.car.po.dto.address.Result;
import com.cxt.car.util.baseUtils.JsonUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @Author: yansf
* @Description:根据经纬度获取地址
* @Date:Creat in 20:20 2019/12/11
* @Modified By:
*/
public class AddressUntils {
    static String secretKey = "此处是申请的key";
    /**
     * @param lat 纬度
     * @param lng 经度
     * @return
     */
    public static Result getAddress(String lat, String lng) throws IOException {
        JSONObject obj = getLocationInfo(lat, lng).getJSONObject("result");
        System.out.println(obj);
        Result result = JsonUtil.fromJson(obj.toString(), Result.class);
        return result;
    }
    public static JSONObject getLocationInfo(String lat, String lng) throws IOException {
        String urlString = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + secretKey;
        System.out.println("请求经纬度url:" + urlString);
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setRequestMethod("GET");
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
        String line;
        String res = "";
        while ((line = in.readLine()) != null) {
            res += line + "\n";
        }
        in.close();
        JSONObject jsonObject = JSONObject.parseObject(res);
        return jsonObject;
    }
    public static void main(String[] args) throws IOException {
        Result address = getAddress("30.2084", "120.21201");
        System.out.println(address.ad_info.city);
    }
}
返回的json串:
{"address":"浙江省杭州市滨江区人民路6","ad_info":{"province":"浙江省","adcode":"330108","nation":"中国","city":"杭州市","district":"滨江区","name":"中国,浙江省,杭州市,滨江区","city_code":"156330100","location":{"lng":120.220459,"lat":30.16667},"nation_code":"156"},"address_reference":{"town":{"_distance":0,"_dir_desc":"","location":{"lng":120.233566,"lat":30.183102},"id":"330108001","title":"西兴街道"},"street":{"_distance":8.6,"_dir_desc":"","location":{"lng":120.206032,"lat":30.204729},"id":"1172092355888263234","title":"新月路"},"crossroad":{"_distance":193,"_dir_desc":"","location":{"lng":120.21003,"lat":30.208891},"id":"6417297","title":"丹枫路/泰安路(路口)"},"street_number":{"_distance":0,"_dir_desc":"","location":{"lng":120.211739,"lat":30.20863},"id":"6709388790419249714","title":"人民路6"},"landmark_l2":{"_distance":0,"_dir_desc":"","location":{"lng":120.211739,"lat":30.20863},"id":"13795638749391557063","title":"滨江区政府"}},"location":{"lng":120.21201,"lat":30.2084},"formatted_addresses":{"rough":"滨江区政府(新月路北)","recommend":"滨江区政府(新月路北)"},"address_component":{"province":"浙江省","nation":"中国","city":"杭州市","street":"人民路","district":"滨江区","street_number":"人民路6"}}
代码中还用到了一个工具类,将json转换成实体类
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
function(){ //外汇返佣 http://www.kaifx.cn/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* @Author: yansf
* @Description:JSON工具类
* @Date:Creat in 17:22 2019/12/10
* @Modified By:
*/
public class JsonUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    /**
     * POJO转换为JSON
     */
    public static <T> String toJson(T obj) {
        String json;
        try {
            json = OBJECT_MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            LOGGER.error("convert POJO to JSON failure", e);
            throw new RuntimeException(e);
            //e.printStackTrace();
        }
        return json;
    }
    /**
     * JSON转为POJO
     */
    public static <T> T fromJson(String json, Class<T> type) {
        T pojo;
        try {
            pojo = OBJECT_MAPPER.readValue(json, type);
        } catch (IOException e) {
            LOGGER.error("convert JSON to POJO failure", e);
            throw new RuntimeException(e);
            //e.printStackTrace();
        }
        return pojo;
    }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马