google天气预报实现

public class WeatherActivity extends Activity {
LinearLayout threeFLayout;
Button threeBtn,returnbtn;
List<WeatherBean> weatherBeans;
WeatherBean weatherBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.weather);
threeBtn=(Button) findViewById(R.id.nextThreeDayBtn);
threeFLayout=(LinearLayout) findViewById(R.id.threeDayWeatherF);
threeBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
threeBtn.setVisibility(View.GONE);
threeFLayout.setVisibility(0);

}
});
returnbtn=(Button) findViewById(R.id.returnbtn);
returnbtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

finish();
}
});
//天气数据显示
//当天
weatherBeans=getCurrentWeather();
// TextView windCondition=(TextView) findViewById(R.id.windVaule);
// windCondition.setText(weatherBean.getWindCondition());
ImageView icon=(ImageView) findViewById(R.id.weatherIma);
icon.setImageDrawable(weatherBeans.get(0).getIcon());

TextView condition=(TextView) findViewById(R.id.weatherText);
condition.setText(weatherBeans.get(0).getCondition());

TextView low=(TextView) findViewById(R.id.lowTptVaule);
low.setText(weatherBeans.get(0).getLow()+"℃");

TextView high=(TextView) findViewById(R.id.highTptVaule);
high.setText(weatherBeans.get(0).getHigh()+"℃");

//后一天
TextView dayOfWeek1=(TextView) findViewById(R.id.dayOfWeek1);
dayOfWeek1.setText(weatherBeans.get(1).getDayOfWeek()+"天气");

ImageView icon1=(ImageView) findViewById(R.id.weatherIma1);
icon1.setImageDrawable(weatherBeans.get(1).getIcon());

TextView condition1=(TextView) findViewById(R.id.weatherText1);
condition1.setText(weatherBeans.get(1).getCondition());

TextView low1=(TextView) findViewById(R.id.lowTptVaule1);
low1.setText(weatherBeans.get(1).getLow()+"℃");

TextView high1=(TextView) findViewById(R.id.highTptVaule1);
high1.setText(weatherBeans.get(1).getHigh()+"℃");

//后二天
TextView dayOfWeek2=(TextView) findViewById(R.id.dayOfWeek2);
dayOfWeek2.setText(weatherBeans.get(2).getDayOfWeek()+"天气");

ImageView icon2=(ImageView) findViewById(R.id.weatherIma2);
icon2.setImageDrawable(weatherBeans.get(2).getIcon());

TextView condition2=(TextView) findViewById(R.id.weatherText2);
condition2.setText(weatherBeans.get(2).getCondition());

TextView low2=(TextView) findViewById(R.id.lowTptVaule2);
low2.setText(weatherBeans.get(2).getLow()+"℃");

TextView high2=(TextView) findViewById(R.id.highTptVaule2);
high2.setText(weatherBeans.get(2).getHigh()+"℃");

//后三天
TextView dayOfWeek3=(TextView) findViewById(R.id.dayOfWeek3);
dayOfWeek3.setText(weatherBeans.get(3).getDayOfWeek()+"天气");
ImageView icon3=(ImageView) findViewById(R.id.weatherIma3);
icon3.setImageDrawable(weatherBeans.get(3).getIcon());

TextView condition3=(TextView) findViewById(R.id.weatherText3);
condition3.setText(weatherBeans.get(3).getCondition());

TextView low3=(TextView) findViewById(R.id.lowTptVaule3);
low3.setText(weatherBeans.get(3).getLow()+"℃");

TextView high3=(TextView) findViewById(R.id.highTptVaule3);
high3.setText(weatherBeans.get(3).getHigh()+"℃");



}

/**
* 通过解析xml数据得到天气信息
* @param url: http://www.google.com/ig/api?&weather=chongqing
* @return
*/
public static List<WeatherBean> getCurrentWeather(){
Document document = null;
String url="http://www.google.com/ig/api?weather=chongqing&hl=zh-cn";
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputStream stream = getStream(url);
//通过InputStreamReader设定编码方式
if (stream == null)
return null;
InputStreamReader streamReader = new InputStreamReader(stream, "GBK");
document = builder.parse(new InputSource(streamReader));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

List<WeatherBean> weatherBeans=new ArrayList<WeatherBean>();

// 当天天气
for(int i=0;i<4;i++)
{
WeatherBean weather= new WeatherBean();
NodeList nodeList = document.getElementsByTagName("forecast_conditions").item(i).getChildNodes(); // 当前天气
String dayOfWeek=nodeList.item(0).getAttributes().item(0).getNodeValue(); 
String low = nodeList.item(1).getAttributes().item(0).getNodeValue(); // 华氏度
String high = nodeList.item(2).getAttributes().item(0).getNodeValue(); // 摄氏度
String imgUrl = nodeList.item(3).getAttributes().item(0).getNodeValue(); // 天气图片
String condition = nodeList.item(4).getAttributes().item(0).getNodeValue();   // 天气情况


weather.setDayOfWeek(dayOfWeek);
weather.setLow(Integer.parseInt(low));
weather.setHigh(Integer.parseInt(high));
weather.setIcon(loadImage(imgUrl));
weather.setCondition(condition);// 解析图片

weatherBeans.add(weather);

}
return weatherBeans;
}
/**
* 得到数据
* @param args
* @return
*/
public final static InputStream getStream(String args) {
InputStream stream = null;
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(args);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
stream = entity.getContent();
}
return stream;
} catch (Exception e) {
e.printStackTrace();
return stream;
}

}

  public final static Drawable loadImage(String url) {
       
       try {
           
           return Drawable.createFromStream((InputStream) new URL("http://www.google.com/" + url).getContent(), "test");
           
       } catch (MalformedURLException e) {
           
           Log.e("exception",e.getMessage());
       } catch (IOException e) {
           
           Log.e("exception",e.getMessage());
       }
       
       return null;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值