java调用天气预报api怎么写
2016-06-24 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
//通过中国天气api调用
private String getWeatherInfo2(){
StringBuilder info = new StringBuilder();
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httget = new HttpGet("http://m.weather.com.cn/data/101050101.html");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httget, responseHandler);
System.out.println(responseBody);
JsonParser jp = new JsonParser();
JsonElement jse = jp.parse(responseBody);
JsonObject jso = jse.getAsJsonObject().get("weatherinfo").getAsJsonObject();
// String updTime = jso.get("fchh").getAsString();
// if(updTime != null){
// //温度
// String j = jso.get("temp1").getAsString();//今天
// String m = jso.get("temp2").getAsString();//明天
// //天气情况
// String j_weather = jso.get("weather1").getAsString();//今天
// String m_weather = jso.get("weather2").getAsString();//明天
// //风向风力
// String j_wind = jso.get("wind1").getAsString();//今天
// String m_wind = jso.get("wind2").getAsString();//明天
// info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
// info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
// }
String updTime = jso.get("fchh").getAsString();
if(updTime != null){
if(!updTime.trim().equals("18")){
//温度
String j = jso.get("temp1").getAsString();//今天
String m = jso.get("temp2").getAsString();//明天
//天气情况
String j_weather = jso.get("weather1").getAsString();//今天
String m_weather = jso.get("weather2").getAsString();//明天
//风向风力
String j_wind = jso.get("wind1").getAsString();//今天
String m_wind = jso.get("wind2").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}else{
//18
//温度
String temp1 = jso.get("temp1").getAsString();//今天
String temp2 = jso.get("temp2").getAsString();//今天
String temp3 = jso.get("temp3").getAsString();//今天
String j = temp1.split("~")[1] + "~" + temp2.split("~")[0];
String m = temp2.split("~")[1] + "~" + temp3.split("~")[0];//明天
//天气情况
String weather1 = jso.get("weather1").getAsString();
String weather2 = jso.get("weather2").getAsString();
String weather3 = jso.get("weather3").getAsString();
String j_weather = "";
String j_weather_part1 = "";
String j_weather_part2 = "";
//判断是否有转
if(weather1.indexOf("转") > 0){
//有
j_weather_part1 = weather1.split("转")[1];
}else{
j_weather_part1 = weather1;
}
if(weather2.indexOf("转") > 0){
//有
j_weather_part2 = weather2.split("转")[0];
}else{
j_weather_part2 = weather2;
}
if(j_weather_part1.equalsIgnoreCase(j_weather_part2)){
j_weather = j_weather_part1;//今天
}else{
j_weather = j_weather_part1 + "转" + j_weather_part2;//今天
}
String m_weather = "";
String m_weather_part1 = "";
String m_weather_part2 = "";
//判断是否有转
if(weather2.indexOf("转") > 0){
//有
m_weather_part1 = weather2.split("转")[1];
}else{
m_weather_part1 = weather2;
}
if(weather3.indexOf("转") > 0){
//有
m_weather_part2 = weather3.split("转")[0];
}else{
m_weather_part2 = weather3;
}
if(m_weather_part1.equalsIgnoreCase(m_weather_part2)){
m_weather = m_weather_part1;//今天
}else{
m_weather = m_weather_part1 + "转" + m_weather_part2;//明天
}
//风向风力
String j_wind = jso.get("wind2").getAsString();//今天
String m_wind = jso.get("wind3").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}
}
} catch (Exception e) {
}
return info.toString();
}
private String getWeatherInfo2(){
StringBuilder info = new StringBuilder();
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httget = new HttpGet("http://m.weather.com.cn/data/101050101.html");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httget, responseHandler);
System.out.println(responseBody);
JsonParser jp = new JsonParser();
JsonElement jse = jp.parse(responseBody);
JsonObject jso = jse.getAsJsonObject().get("weatherinfo").getAsJsonObject();
// String updTime = jso.get("fchh").getAsString();
// if(updTime != null){
// //温度
// String j = jso.get("temp1").getAsString();//今天
// String m = jso.get("temp2").getAsString();//明天
// //天气情况
// String j_weather = jso.get("weather1").getAsString();//今天
// String m_weather = jso.get("weather2").getAsString();//明天
// //风向风力
// String j_wind = jso.get("wind1").getAsString();//今天
// String m_wind = jso.get("wind2").getAsString();//明天
// info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
// info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
// }
String updTime = jso.get("fchh").getAsString();
if(updTime != null){
if(!updTime.trim().equals("18")){
//温度
String j = jso.get("temp1").getAsString();//今天
String m = jso.get("temp2").getAsString();//明天
//天气情况
String j_weather = jso.get("weather1").getAsString();//今天
String m_weather = jso.get("weather2").getAsString();//明天
//风向风力
String j_wind = jso.get("wind1").getAsString();//今天
String m_wind = jso.get("wind2").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}else{
//18
//温度
String temp1 = jso.get("temp1").getAsString();//今天
String temp2 = jso.get("temp2").getAsString();//今天
String temp3 = jso.get("temp3").getAsString();//今天
String j = temp1.split("~")[1] + "~" + temp2.split("~")[0];
String m = temp2.split("~")[1] + "~" + temp3.split("~")[0];//明天
//天气情况
String weather1 = jso.get("weather1").getAsString();
String weather2 = jso.get("weather2").getAsString();
String weather3 = jso.get("weather3").getAsString();
String j_weather = "";
String j_weather_part1 = "";
String j_weather_part2 = "";
//判断是否有转
if(weather1.indexOf("转") > 0){
//有
j_weather_part1 = weather1.split("转")[1];
}else{
j_weather_part1 = weather1;
}
if(weather2.indexOf("转") > 0){
//有
j_weather_part2 = weather2.split("转")[0];
}else{
j_weather_part2 = weather2;
}
if(j_weather_part1.equalsIgnoreCase(j_weather_part2)){
j_weather = j_weather_part1;//今天
}else{
j_weather = j_weather_part1 + "转" + j_weather_part2;//今天
}
String m_weather = "";
String m_weather_part1 = "";
String m_weather_part2 = "";
//判断是否有转
if(weather2.indexOf("转") > 0){
//有
m_weather_part1 = weather2.split("转")[1];
}else{
m_weather_part1 = weather2;
}
if(weather3.indexOf("转") > 0){
//有
m_weather_part2 = weather3.split("转")[0];
}else{
m_weather_part2 = weather3;
}
if(m_weather_part1.equalsIgnoreCase(m_weather_part2)){
m_weather = m_weather_part1;//今天
}else{
m_weather = m_weather_part1 + "转" + m_weather_part2;//明天
}
//风向风力
String j_wind = jso.get("wind2").getAsString();//今天
String m_wind = jso.get("wind3").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}
}
} catch (Exception e) {
}
return info.toString();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询