public static StringBuffer httpsRequest(String requestUrl, String requestMethod, String output) throws IOException,
UnsupportedEncodingException {
URL url = new URL(requestUrl);
if (requestUrl.startsWith("https")) {
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod(requestMethod);
if (null != output) {
OutputStream outputStream = connection.getOutputStream();
outputStream.write(output.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str.trim());
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
connection.disconnect();
return buffer;
} else {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod(requestMethod);
if (null != output) {
OutputStream outputStream = connection.getOutputStream();
outputStream.write(output.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str.trim());
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
connection.disconnect();
return buffer;
}
}
public List<StationInfo> getStationByLngLat(String lng, String lat, int range){
int radius=range;//搜索的半径范围
String url="http://api.map.baidu.com/place/v2/search?query=地铁站&page_size=10&page_num=0&scope=1&location="+lat+","+lng+"&radius="+radius+"&output=json&ak={百度服务API秘钥}";
List<StationInfo> list=new ArrayList<StationInfo>();
// System.out.println(HttpConnectionUtil.httpsRequest(url, "GET", null).toString());
JSONObject jsonObject= JSONObject.fromObject( HttpConnectionUtil.httpsRequest(url, "GET", null).toString());
// System.out.println("我是:"+jsonObject.get("results"));
// System.out.println("我是2:"+jsonObject.get("total"));
JSONArray arrays=jsonObject.getJSONArray("results");
if(arrays==null){//如果没有则返回空
for (int i=0;i<arrays.size();i++){
StationInfo stationInfo=new StationInfo();
JSONObject jsonObject1=JSONObject.fromObject(arrays.getJSONObject(i));
stationInfo.setLat(jsonObject1.getJSONObject("location").get("lat").toString());
stationInfo.setLat(jsonObject1.getJSONObject("location").get("lng").toString());
stationInfo.setStationName(jsonObject1.get("name").toString());
String subwayStr=jsonObject1.get("address").toString();
String[] subways=subwayStr.split(";");
stationInfo.setSubway(subways[0]);
} catch (IOException e) {