第一种方法:
public static void main(String[] args) { try { String lon = "113.540124"; String lat = "23.517846"; JSONObject json = getAllEmployee(lon, lat); String result = json.getString("result");
System.err.println(result);
//添加处理String转为Json
//result = result.substring(1, result.length() - 1);//JSONObject jsonobject = JSONObject.fromObject(result);//System.out.println(jsonobject.getString("x"));//System.out.println(jsonobject.getString("y"));
} catch (Exception e) { e.printStackTrace(); } } /** * 返回JSON格式的数据 * */ public static JSONObject getAllEmployee(String lon, String lat) { String url = "http://api.map.baidu.com/geoconv/v1/?coords=" + lon + "," + lat + ";116.964273,36.545689&from=1&to=5&ak=Dw*******************pGuMG"; try { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); ResponseHandler<JSONObject> responseHandler = new ResponseHandler<JSONObject>() { // 成功调用连接后,对返回数据进行的操作 @Override public JSONObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { // 获得调用成功后 返回的数据 HttpEntity entity = response.getEntity(); if (null != entity) { String result = EntityUtils.toString(entity); // 根据字符串生成JSON对象 JSONObject resultObj = JSONObject.fromObject(result); return resultObj; } else { return null; } } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; JSONObject responseBody = httpclient.execute(httpPost, responseHandler); return responseBody; } catch (Exception e) { e.printStackTrace(); return null; }}
参数说明:
结果:
第二种方法:借鉴别人的博客写的
public static void main(String[] args) { try { String lon = "113.540124"; String lat = "23.517846"; JSONObject json = getAllEmployee(lon, lat); String latitude = json.getString("x"); String longitude = json.getString("y"); System.out.println(decode(latitude));// 113.55180949764 System.out.println(decode(longitude));// 23.52123360177 } catch (Exception e) { e.printStackTrace(); } } /** * 返回JSON格式的数据 * */ public static JSONObject getAllEmployee(String lon, String lat) { String url = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=" + lon + "&y=" + lat; try { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); ResponseHandler<JSONObject> responseHandler = new ResponseHandler<JSONObject>() { // 成功调用连接后,对返回数据进行的操作 @Override public JSONObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { // 获得调用成功后 返回的数据 HttpEntity entity = response.getEntity(); if (null != entity) { String result = EntityUtils.toString(entity); // 根据字符串生成JSON对象 JSONObject resultObj = JSONObject.fromObject(result); return resultObj; } else { return null; } } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; JSONObject responseBody = httpclient.execute(httpPost, responseHandler); return responseBody; } catch (Exception e) { e.printStackTrace(); return null; } } public static String decode(String str) { byte[] bytes = null; String string = ""; try { sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); bytes = decoder.decodeBuffer(str); string = new String(bytes, "GB2312"); } catch (IOException e) { e.printStackTrace(); } return string;
}
结果:
所需jar包