我在java中用以下方法调用qq地图的webservice服务 解析地址
URL resjson = new URL("http://apis.map.qq.com/ws/geocoder/v1/?region=" + city + "&address=" + address + "&key=" + QQkey); BufferedReader in = new BufferedReader(new InputStreamReader( resjson.openStream(), "utf-8")); String res; StringBuilder sb = new StringBuilder(""); while ((res = in.readLine()) != null) { sb.append(res.trim()); } in.close(); String str = sb.toString(); System.out.println("return json:" + str); if (str != null && !str.equals("")) { HashMap<String, String> map = null; int lngStart = str.indexOf("lng\":"); int lngEnd = str.indexOf(",\"lat"); int latEnd = str.indexOf("},\"address_components"); if (lngStart > 0 && lngEnd > 0 && latEnd > 0) { String lng = str.substring(lngStart + 5, lngEnd); String lat = str.substring(lngEnd + 7, latEnd); map = new HashMap<String, String>(); map.put("lng", lng.replaceAll("\"", "")); map.put("lat", lat.replaceAll("\"", "")); return map; } }
结果返回的是{status:-1,message:系统异常}
结果发现是 请求链接的编码问题
加上
// 将地址转换成utf-8的16进制 address = URLEncoder.encode(address, "UTF-8"); city = URLEncoder.encode(city, "UTF-8"); // 如果有代理,要设置代理,没代理可注释 // System.setProperty("http.proxyHost","192.168.172.23"); // System.setProperty("http.proxyPort","3209");