java天气预报调用百度接口实现
来源:互联网 发布:软件开发流程图绘制 编辑:程序博客网 时间:2024/06/05 19:59
package com.cn.weather;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class BaiDu {
//根据城市获取天气信息的java代码
//cityName 是你要取得天气信息的城市的中文名字,如“北京”,“深圳”
static String getWeatherInform(String cityName){
//百度天气API
String baiduUrl = null;
try {
baiduUrl = "http://api.map.baidu.com/telematics/v3/weather?location="+URLEncoder.encode(cityName, "utf-8")+"&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH";
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuffer strBuf;
strBuf = new StringBuffer();
try{
URL url = new URL(baiduUrl);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));//转码。
String line = null;
while ((line = reader.readLine()) != null)
strBuf.append(line + " ");
reader.close();
}catch(MalformedURLException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return strBuf.toString();
}
public static void main(String[] args){
BaiDu baidu = new BaiDu();
System.out.println(baidu.getWeatherInform("海淀"));
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class BaiDu {
//根据城市获取天气信息的java代码
//cityName 是你要取得天气信息的城市的中文名字,如“北京”,“深圳”
static String getWeatherInform(String cityName){
//百度天气API
String baiduUrl = null;
try {
baiduUrl = "http://api.map.baidu.com/telematics/v3/weather?location="+URLEncoder.encode(cityName, "utf-8")+"&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH";
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuffer strBuf;
strBuf = new StringBuffer();
try{
URL url = new URL(baiduUrl);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));//转码。
String line = null;
while ((line = reader.readLine()) != null)
strBuf.append(line + " ");
reader.close();
}catch(MalformedURLException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return strBuf.toString();
}
public static void main(String[] args){
BaiDu baidu = new BaiDu();
System.out.println(baidu.getWeatherInform("海淀"));
}
}
0 0
- java天气预报调用百度接口实现
- Java 调用webservice 实现天气预报
- Flex 调用WEBService 接口实现天气预报功能.
- 全国天气预报接口JAVA调用示例
- 百度天气预报接口
- 百度天气预报接口
- 百度天气预报接口
- 百度天气预报接口
- 百度天气预报接口
- 百度天气预报接口介绍
- 百度天气预报接口
- 百度天气预报接口
- 百度天气预报接口介绍
- 百度天气预报接口介绍
- 百度天气预报接口介绍
- 中央气象局天气预报接口---java实现
- 中央气象局天气预报接口---java实现
- 调用接口进行天气预报
- JAVA_日期处理
- 2016的开始
- mysql索引总结----mysql 索引类型以及创建
- Spring框架中如何读取Spring的XML配置文件
- CentOS服务器,Nginx+Tomcat 上部署javaEE项目,负载均衡
- java天气预报调用百度接口实现
- CSS里的单位介绍
- YLD一键还原工具
- view的生命周期
- 代码混淆
- ODE的线程模型
- qq 微信 launcher,如何跳转到上千款应用
- viewpager最后一页滑动切换到其他activity
- python 实例8 日常记账本小程序