@Test public void testRequestApi() { String apiUrl = "http: String appKey = "XXXXXXXX"; String secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; Map<String, String> paramMap = new HashMap<String, String>(); paramMap.put("city", "上海"); paramMap.put("latitude", "31.21524"); paramMap.put("longitude", "121.420033"); paramMap.put("category", "美食"); paramMap.put("region", "长宁区"); paramMap.put("limit", "20"); paramMap.put("radius", "2000"); paramMap.put("offset_type", "0"); paramMap.put("has_coupon", "1"); paramMap.put("has_deal", "1"); paramMap.put("keyword", "泰国菜"); paramMap.put("sort", "7"); paramMap.put("format", "json"); String requestResult = DemoApiTool.requestApi(apiUrl, appKey, secret, paramMap); LOGGER.info(requestResult); Assert.assertNotNull(requestResult); }
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"); LOGGER.debug("Encoded Query:" + encodeQuery); 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) { LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e); } catch (IOException e) { LOGGER.error("Request URL: " + apiUrl + " failed. ", e); } finally { method.releaseConnection(); } return response.toString(); }
DemoApiTool { 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"); LOGGER.debug("Encoded Query:" + encodeQuery); 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) { LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e); } catch (IOException e) { LOGGER.error("Request URL: " + apiUrl + " failed. ", e); } finally { method.releaseConnection(); } return response.toString(); } }
DemoApiTool { 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"); LOGGER.debug("Encoded Query:" + encodeQuery); 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) { LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e); } catch (IOException e) { LOGGER.error("Request URL: " + apiUrl + " failed. ", e); } finally { method.releaseConnection(); } return response.toString(); }  }
DemoApiTool { 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"); LOGGER.debug("Encoded Query:" + encodeQuery); 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) { LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e); } catch (IOException e) { LOGGER.error("Request URL: " + apiUrl + " failed. ", e); } finally { method.releaseConnection(); } return response.toString(); }  static String sign(String appKey, String secret, Map<String, String> paramMap); static String getQueryString(String appKey, String secret, Map<String, String> paramMap); static String requestApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap); static String requestPostApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap); }
DemoApiTool { 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"); LOGGER.debug("Encoded Query:" + encodeQuery); 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) { LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e); } catch (IOException e) { LOGGER.error("Request URL: " + apiUrl + " failed. ", e); } finally { method.releaseConnection(); } return response.toString(); }  static String sign(String appKey, String secret, Map<String, String> paramMap); static String getQueryString(String appKey, String secret, Map<String, String> paramMap); static String requestApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap); static String requestPostApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap);  }