实例介绍
【实例截图】
【核心代码】
package com.demo.pr7;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector;
import org.xmlpull.v1.XmlPullParser;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
public class WebWeatherActivity extends Activity implements Runnable{
/** Called when the activity is first created. */
HttpURLConnection httpConn = null;
InputStream din = null;
Vector<String> cityname=new Vector<String>();
Vector<String> low=new Vector<String>();
Vector<String> high=new Vector<String>();
Vector<String> icon=new Vector<String>();
Vector<Bitmap> bitmap=new Vector<Bitmap>();
Vector<String> summary=new Vector<String>();
int weatherIndex[]=new int[20];
String city="guangzhou";
boolean bPress=false;
boolean bHasData=false;
LinearLayout body;
Button find;
EditText value;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("天气查询");
body=(LinearLayout)findViewById(R.id.my_body);
find=(Button)findViewById(R.id.find);
value=(EditText)findViewById(R.id.value);
find.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
body.removeAllViews();//移除当前的所有结果
city=value.getText().toString();
Toast.makeText(WebWeatherActivity.this,
"正在查询天气信息...", Toast.LENGTH_LONG).show();
Thread th=new Thread(WebWeatherActivity.this);
th.start();
}
});
}
@Override
public void run() {
// TODO Auto-generated method stub
cityname.removeAllElements();
low.removeAllElements();
high.removeAllElements();
icon.removeAllElements();
bitmap.removeAllElements();
summary.removeAllElements();
//获取数据
parseData();
//下载图片
downImage();
//通知UI线程显示结果
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
//获取数据
public void parseData(){
int i=0;
String sValue;
//city变量表示城市名字的拼音
String weatherUrl="http://flash.weather.com.cn/wmaps/xml/" city ".xml";
//表示天气情况图标的基础网址
String weatherIcon="http://m.weather.com.cn/img/c";
try
{
URL url=new URL(weatherUrl);
//建立天气预报查询连接
httpConn =(HttpURLConnection)url.openConnection();
//采用GET请求方法
httpConn.setRequestMethod("GET");
//打开数据输入流
din=httpConn.getInputStream();
InputStreamReader in=new InputStreamReader(httpConn.getInputStream());
// 为输出创建BufferedReader
BufferedReader buffer=new BufferedReader(in);
String inputLine="";
String resultData="";
//使用循环来读取获得的数据
while((inputLine=buffer.readLine())!=null)
{
//在每一行后面加上一个"\n"来换行
resultData =inputLine "\n";
}
//获得XmlPullParser解析器
XmlPullParser xmlParser=Xml.newPullParser();
ByteArrayInputStream tInputStringStream=null;
//获得解析到的事件类别,这里有开始文档,结束文档,开始标签,结束标签,文本等等事件。
tInputStringStream=new ByteArrayInputStream(resultData.getBytes());
xmlParser.setInput(tInputStringStream, "UTF-8");
int evtType=xmlParser.getEventType();
while(evtType!=XmlPullParser.END_DOCUMENT)//一直循环,直到文档结束
{
switch(evtType)
{
case XmlPullParser.START_TAG:
String tag = xmlParser.getName();
//如果是city标签开始,则说明需要实例化对象了
if (tag.equalsIgnoreCase("city"))
{
//城市天气预报
cityname.addElement(xmlParser.getAttributeValue(null, "cityname") "天气:");
//天气情况概述
summary.addElement(xmlParser.getAttributeValue(null, "stateDetailed"));
//最低温度
low.addElement("最低:" xmlParser.getAttributeValue(null, "tem2"));
//最高温度
high.addElement("最高:" xmlParser.getAttributeValue(null, "tem1"));
//天气情况图标网址
icon.addElement(weatherIcon xmlParser.getAttributeValue(null, "state1") ".gif");
}
break;
case XmlPullParser.END_TAG:
//标签结束
default:break;
}
//如果xml没有结束,则导航到下一个节点
evtType=xmlParser.next();
}
}catch (Exception ex){
ex.printStackTrace();
}finally{
//释放连接
try{
din.close();
httpConn.disconnect();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
// 下载图片
private void downImage()
{
//天气情况图标获取
int i=0;
for(i=0;i<icon.size();i ){
try
{
URL url=new URL(icon.elementAt(i));
System.out.println(icon.elementAt(i));
httpConn =(HttpURLConnection)url.openConnection();
httpConn.setRequestMethod("GET");
din =httpConn.getInputStream();
//图片数据Bitmap
bitmap.addElement(BitmapFactory.decodeStream(httpConn.getInputStream()));
}catch (Exception ex){
ex.printStackTrace();
}finally{
//释放连接
try{
din.close();
httpConn.disconnect();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
//显示结果handler
private final Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
showData();
break;
}
super.handleMessage(msg);
}
};
//显示结果
public void showData(){
body.removeAllViews();//清除存储旧的查询结果的组件
body.setOrientation(LinearLayout.VERTICAL);
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.weight=80;
params.height=50;
for(int i=0;i<cityname.size();i ){
LinearLayout linerlayout=new LinearLayout(this);
linerlayout.setOrientation(LinearLayout.HORIZONTAL);
//城市
TextView dayView=new TextView(this);
dayView.setLayoutParams(params);
dayView.setText(cityname.elementAt(i));
linerlayout.addView(dayView);
//描述
TextView summaryView=new TextView(this);
summaryView.setLayoutParams(params);
summaryView.setText(summary.elementAt(i));
linerlayout.addView(summaryView);
//图标
ImageView icon=new ImageView(this);
icon.setLayoutParams(params);
icon.setImageBitmap(bitmap.elementAt(i));
linerlayout.addView(icon);
//最低气温
TextView lowView=new TextView(this);
lowView.setLayoutParams(params);
lowView.setText(low.elementAt(i));
linerlayout.addView(lowView);
//最高气温
TextView highView=new TextView(this);
highView.setLayoutParams(params);
highView.setText(high.elementAt(i));
linerlayout.addView(highView);
body.addView(linerlayout);
}
}
}
标签: 天气
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论