1:辅助类
package com.tgb.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.URIException; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.httpclient.util.URIUtil; import org.apache.commons.lang.StringUtils; public class DzdpUtil { public static String sign(String appKey, String secret, Map<String, String> paramMap) { String[] keyArray = paramMap.keySet().toArray(new String[0]); Arrays.sort(keyArray); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(appKey); for (String key : keyArray) { stringBuilder.append(key).append(paramMap.get(key)); } stringBuilder.append(secret); String codes = stringBuilder.toString(); String sign = org.apache.commons.codec.digest.DigestUtils.shaHex(codes) .toUpperCase(); return sign; } public static String getQueryString(String appKey, String secret, Map<String, String> paramMap) { String sign = sign(appKey, secret, paramMap); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("appkey=").append(appKey).append("&sign=") .append(sign); for (Entry<String, String> entry : paramMap.entrySet()) { stringBuilder.append('&').append(entry.getKey()).append('=') .append(entry.getValue()); } String queryString = stringBuilder.toString(); return queryString; } public static String requestApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap) { String queryString = getQueryString(appKey, secret, paramMap); StringBuffer response = new StringBuffer(); HttpClientParams httpConnectionParams = new HttpClientParams(); httpConnectionParams.setConnectionManagerTimeout(1000); HttpClient client = new HttpClient(httpConnectionParams); HttpMethod method = new GetMethod(apiUrl); try { if (StringUtils.isNotBlank(queryString)) { String encodeQuery = URIUtil.encodeQuery(queryString, "UTF-8"); method.setQueryString(encodeQuery); } client.executeMethod(method); BufferedReader reader = new BufferedReader(new InputStreamReader( method.getResponseBodyAsStream(), "UTF-8")); String line = null; while ((line = reader.readLine()) != null) { response.append(line).append( System.getProperty("line.separator")); } reader.close(); } catch (URIException e) { } catch (IOException e) { } finally { method.releaseConnection(); } return response.toString(); } }
2:数据类:
package com.tgb.util; public class DzdpData { public static final String APPKEY = "xx"; public static final String SECRET = "xx"; public static final String CITY = ""; public static final String CATEGORY = "酒店"; public static final String CharSet = "UTF-8"; }
3:请求接口
@RequestMapping(value="/address") public String getMap(HttpServletRequest request ,Model model) throws ParseException, IOException { String x = "136.11111"; String y = "26.36845"; String apiUrl = "http://api.dianping.com/v1/business/find_businesses"; String appKey = DzdpData.APPKEY; String secret = DzdpData.SECRET; Map<String, String> paramMap = new HashMap<String, String>(); paramMap.put("latitude", x); paramMap.put("longitude",y); paramMap.put("category", DzdpData.CATEGORY); paramMap.put("limit", "20"); paramMap.put("offset_type", "0"); paramMap.put("sort", "7"); paramMap.put("format", "json"); paramMap.put("radius", "2000"); String json = DzdpUtil.requestApi(apiUrl, appKey, secret, paramMap); model.addAttribute("json",json); return "页面"; }
转载于:https://www.cnblogs.com/MedivhQ/p/4101262.html