All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liuchink.chinkutils.baidu.ChinkBaiDuMapPlaceSearch2Utils Maven / Gradle / Ivy

There is a newer version: 0.0.14.RELEASE
Show newest version
package com.liuchink.chinkutils.baidu;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.liuchink.chinkutils.baidu.bean.placesearch.*;
import com.liuchink.chinkutils.json.ChinkFastJsonUtils;
import com.liuchink.chinkutils.net.ChinkOkHttpUtils;
import com.liuchink.chinkutils.net.bean.HttpResponse;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.CharSequenceUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 地点检索V2.0
 *
 * @author liugang
 * @since 2020/9/21 15:35
 */
@Slf4j
public class ChinkBaiDuMapPlaceSearch2Utils {

	private ChinkBaiDuMapPlaceSearch2Utils() {

	}

	/**
	 * 地点检索
	 *
	 * @param placeSearchRegionReq
	 *            placeSearchRegionReq
	 * @return com.liuchink.chinkutils.baidu.map.bean.placesearch.PlaceSearchResponse
	 * @author liugang
	 * @since 2020/9/21 15:48
	 */
	public static PlaceSearchResp placeSearchRegion(
			PlaceSearchRegionReq placeSearchRegionReq)
			throws NoSuchAlgorithmException, IOException,
			KeyManagementException {
		StringBuilder url = new StringBuilder(
				"https://api.map.baidu.com/place/v2/search?");
		for (Map.Entry stringEntry : JSON
				.parseObject(
						ChinkFastJsonUtils.toJsonString(placeSearchRegionReq))
				.entrySet()) {
			if (StringUtils.isNotBlank(Convert.toStr(stringEntry.getValue()))) {
				url.append(stringEntry.getKey()).append("=")
						.append(stringEntry.getValue()).append("&");
			}
		}
		String postUrl = CharSequenceUtil.removeSuffix(url, "&");
		log.debug(":请求报文:【{}】", postUrl);
		HttpResponse httpResponse = ChinkOkHttpUtils.doHttpGet(postUrl);
		PlaceSearchResp placeSearchBaseResp = new PlaceSearchResp<>();
		log.debug(":应答报文:【{}】",
				httpResponse.getResult().getBody());
		if (httpResponse.getStatus().getCode() == 200) {
			if ("json".equals(placeSearchRegionReq.getOutput())) {
				placeSearchBaseResp = JSON.parseObject(
						httpResponse.getResult().getBody(),
						new TypeReference>() {
						});
			} else {
				placeSearchBaseResp
						.setXmlResult(httpResponse.getResult().getBody());
			}

		} else {
			placeSearchBaseResp.setStatus(httpResponse.getStatus().getCode());
			placeSearchBaseResp.setMessage("网络请求错误");
		}
		return placeSearchBaseResp;
	}

	/**
	 * 地点检索
	 *
	 * @param placeSearchRadiusReq
	 *            placeSearchRadiusReq
	 * @return com.liuchink.chinkutils.baidu.map.bean.placesearch.PlaceSearchResponse
	 * @author liugang
	 * @since 2020/9/21 15:48
	 */
	public static PlaceSearchResp placeSearchRadius(
			PlaceSearchRadiusReq placeSearchRadiusReq)
			throws NoSuchAlgorithmException, IOException,
			KeyManagementException {
		StringBuilder url = new StringBuilder(
				"https://api.map.baidu.com/place/v2/search?");
		for (Map.Entry stringEntry : JSON
				.parseObject(
						ChinkFastJsonUtils.toJsonString(placeSearchRadiusReq))
				.entrySet()) {
			if (StringUtils.isNotBlank(Convert.toStr(stringEntry.getValue()))) {
				url.append(stringEntry.getKey()).append("=")
						.append(stringEntry.getValue()).append("&");
			}
		}
		String postUrl = CharSequenceUtil.removeSuffix(url, "&");
		log.debug(":请求报文:【{}】", postUrl);
		HttpResponse httpResponse = ChinkOkHttpUtils.doHttpGet(postUrl);
		PlaceSearchResp placeSearchBaseResp = new PlaceSearchResp<>();
		log.debug(":应答报文:【{}】",
				httpResponse.getResult().getBody());
		if (httpResponse.getStatus().getCode() == 200) {
			if ("json".equals(placeSearchRadiusReq.getOutput())) {
				placeSearchBaseResp = JSON.parseObject(
						httpResponse.getResult().getBody(),
						new TypeReference>() {
						});
			} else {
				placeSearchBaseResp
						.setXmlResult(httpResponse.getResult().getBody());
			}
		} else {
			placeSearchBaseResp.setStatus(httpResponse.getStatus().getCode());
			placeSearchBaseResp.setMessage("网络请求错误");
		}
		return placeSearchBaseResp;
	}

	/**
	 * 地点检索
	 *
	 * @param placeSearchDetailReq
	 *            placeSearchRadiusReq
	 * @return com.liuchink.chinkutils.baidu.map.bean.placesearch.PlaceSearchResponse
	 * @author liugang
	 * @since 2020/9/21 15:48
	 */
	public static PlaceSearchResp placeSearchDetail(
			PlaceSearchDetailReq placeSearchDetailReq)
			throws NoSuchAlgorithmException, IOException,
			KeyManagementException {
		StringBuilder url = new StringBuilder(
				"https://api.map.baidu.com/place/v2/detail?");
		for (Map.Entry stringEntry : JSON
				.parseObject(
						ChinkFastJsonUtils.toJsonString(placeSearchDetailReq))
				.entrySet()) {
			if (StringUtils.isNotBlank(Convert.toStr(stringEntry.getValue()))) {
				url.append(stringEntry.getKey()).append("=")
						.append(stringEntry.getValue()).append("&");
			}
		}
		String postUrl = CharSequenceUtil.removeSuffix(url, "&");
		log.debug(":请求报文:【{}】", postUrl);
		HttpResponse httpResponse = ChinkOkHttpUtils.doHttpGet(postUrl);
		PlaceSearchResp placeSearchBaseResp = new PlaceSearchResp<>();
		log.debug(":应答报文:【{}】",
				httpResponse.getResult().getBody());
		if (httpResponse.getStatus().getCode() == 200) {
			if ("json".equals(placeSearchDetailReq.getOutput())) {
				placeSearchBaseResp = JSON.parseObject(
						httpResponse.getResult().getBody(),
						new TypeReference>() {
						});
			} else {
				placeSearchBaseResp
						.setXmlResult(httpResponse.getResult().getBody());
			}
		} else {
			placeSearchBaseResp.setStatus(httpResponse.getStatus().getCode());
			placeSearchBaseResp.setMessage("网络请求错误");
		}
		return placeSearchBaseResp;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy