由于工作需要,最近写了个java项目,获取天气预报存入数据库。
step1:
创建一个java项目。
项目结构如下:
step2:
创建一个WeatherUtil
package util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;
public class WeatherUtil {
/**
* 获取天气预报信息
* @throws UnsupportedEncodingException
*/
public static String getWeather(String url) throws UnsupportedEncodingException{
URL realUrl = null;
ByteArrayOutputStream out = null;
try {
//真实地址
realUrl = new URL(url);
//打开连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
//设置连接属性
connection.setRequestProperty("accept", "application/xhtml+xml,application/json,application/xml;charset=utf-8, text/javascript, */*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("contentType", "utf-8");
connection.setRequestMethod("GET");
connection.setRequestProperty("user-agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
//FileOutputStream out = new FileOutputStream("e:/text.txt");
//这里获取的数据时压缩格式的数据所以用gzip进行解压缩
GZIPInputStream gip = new GZIPInputStream(connection.getInputStream());
out = new ByteArrayOutputStream();
//缓冲
byte []buffer = new byte[1024];
int len ;
while((len = gip.read(buffer))!=-1){
out.write(buffer, 0, len);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭流
try {
if(out != null){
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//把字节数据转化为字符串返回回去
return (new String(out.toByteArray(), "utf-8"));
}
}
step3:
创建TestWeather
public static void main(String[] args) throws UnsupportedEncodingException {
String url="http://wthrcdn.etouch.cn/WeatherApi?citykey=101040100";
String weather = WeatherUtil.getWeather(url);
System.out.println(weather);
}
就可以在控制台输出获取到的天气预报了。
项目打包下载:http://download.csdn.net/download/qq_40574584/10203397