/**
* JSON ,
*/
public void getTelematics() {
String cityName;
StringBuffer buffer = null;
// , utf-8
cityName = UrlEncoded.encodeString(getPara("cityName"), "UTF-8");
// API key
String key = UrlUtils.getKey("ak");
// url
String requestUrl = "http://api.map.baidu.com/telematics/v3/weather?location="
+ cityName + "&output=json&ak=" + key;
buffer = new StringBuffer();
// url json buffer
UrlUtils.openUrl(requestUrl, buffer);
renderJson(buffer.toString());
}
package util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
public class UrlUtils {
public static String getKey(String keyName){
Properties properties = new Properties();
String keyValue = "";
try{
InputStream in = UrlUtils.class.getResourceAsStream("/keys.properties");
properties.load(in);
keyValue = properties.getProperty(keyName);
}catch(Exception e){
e.printStackTrace();
}
return keyValue ;
}
public static void openUrl(String requestUrl, StringBuffer buffer) {
URL url = null;
HttpURLConnection httpUrlConn = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
url = new URL(requestUrl);
httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoInput(true);
httpUrlConn.setRequestMethod("GET");
//
inputStream = httpUrlConn.getInputStream();
inputStreamReader = new InputStreamReader(inputStream, "utf-8");
bufferedReader = new BufferedReader(inputStreamReader);
//
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
//
bufferedReader.close();
}
inputStreamReader.close();
inputStream.close();
httpUrlConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> </title>
<script type="text/javascript" src="/js/jquery-1.8.0.js"></script>
<script type="text/javascript" src="/js/telematics.js"></script>
</head>
<body>
:
<input id="cityName" type="text" name="cityName">
<input type="button" value=" " onclick="search();">
<hr>
<p> 4 </p>
<table id="tab" border="1px solid red" width="500px">
<tr id="tr">
<td width="100px"> </td>
<td width="100px"> </td>
<td width="100px"> </td>
<td width="100px"> </td>
<td width="50px"> </td>
<td width="50px"> </td>
</tr>
</table>
<hr>
<p> </p>
<table id="tab2" border="1px solid red" width="500px">
</table>
</body>
</html>