标签:sage port city tin line util eth java.net 地址
package com.action; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class test { /** * @Author liuchao email:542178387@qq.com * @data 2017-5-11 * @version 1.0.1 * @param args */ public static String get(String urlAll,String charset){ BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";//模拟浏览器 try { URL url = new URL(urlAll); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(30000); connection.setConnectTimeout(30000); connection.setRequestProperty("User-agent",userAgent); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader( is, charset)); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r"); } reader.close(); result = sbf.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } public static List<Object> jeixi(String msg){ List<Object> list=new ArrayList<Object>(); String json=msg; //JSONParser parser=new JSONParser(); JSONObject obj=JSONObject.fromObject(msg); JSONArray result=(JSONArray) obj.get("result"); JSONObject result1=(JSONObject) result.get(0); JSONObject location=(JSONObject) result1.get("location"); list.add(obj.get("status")); list.add(result1.get("city")); list.add(result1.get("district")); list.add(result1.get("name")); list.add(location.get("lng")); list.add(location.get("lat")); list.add(obj.get("message")); list.add(result1.get("uid")); list.add(result1.get("businesss")); list.add(result1.get("cityid")); list.add(result); return list; } public static void main(String[] args){ // TODO Auto-generated method stub //String url="http://api.map.baidu.com/place/v2/suggestion?query=湖北省武汉市洪山区广埠屯®ion=湖北省&city_limit=true&output=json&ak=gRhqOOqPOQzvM8nMRnVoQswejvggglqY"; String url2="http://api.map.baidu.com/place/v2/suggestion?query=上海市浦东新区锦绣申江®ion=上海市&city_limit=true&output=json&ak=gRhqOOqPOQzvM8nMRnVoQswejvggglqY"; String result=test.get(url2, "UTF-8"); List<Object> result1=test.jeixi(result); for(int i=0;i<result1.size();i++){ System.out.println(result1.get(i)); } } }
标签:sage port city tin line util eth java.net 地址
原文地址:http://www.cnblogs.com/feitianshaoxai/p/6865764.html