life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks
http://www.oschina.net/code/snippet_149945_6389

[代码] [Java]代码
001    public class WeatherActivity extends Activity {
002        private TextView txCity;
003        private Button btnSearch;
004        private Handler weatherhandler;
005        private Dialog progressDialog;
006        private Timer timer;
007        /** Called when the activity is first created. */
008        @Override
009        public void onCreate(Bundle savedInstanceState) {
010            super.onCreate(savedInstanceState);
011            setContentView(R.layout.main);
012            timer = new Timer();
013            txCity = (TextView)findViewById(R.id.txCity);
014            btnSearch = (Button)findViewById(R.id.btnSearch);
015            progressDialog = new AlertDialog.Builder(this)
016            .setTitle("读取数据中")
017            .setMessage("正在加载数据,请稍等")
018            .create();
019             
020            weatherhandler = new Handler(){
021                public void handleMessage(Message msg){
022                    final String cityName = txCity.getText().toString().trim();
023                    searchWeather(cityName);
024                    progressDialog.hide();
025                }
026            };
027             
028            btnSearch.setOnClickListener(new OnClickListener() {
029                 
030                @Override
031                public void onClick(View v) {
032                    progressDialog.show();
033                    timer.schedule(new TimerTask() {
034                        @Override
035                        public void run() {
036                            Message msg = new Message();
037                            msg.setTarget(weatherhandler);
038                            msg.sendToTarget();
039                        }
040                    },100);
041                }
042            });
043        }
044        private void searchWeather(String city){
045            SAXParserFactory spf = SAXParserFactory.newInstance();
046            try {
047                SAXParser sp = spf.newSAXParser();
048                XMLReader reader = sp.getXMLReader();
049                XmlHandler handler = new XmlHandler();
050                reader.setContentHandler(handler);
051                URL url = new URL("http://www.google.com/ig/api?hl=zh-cn&weather="+URLEncoder.encode(city));
052                InputStream is = url.openStream();
053                InputStreamReader isr = new InputStreamReader(is, "GBK");
054                InputSource source = new InputSource(isr);
055                reader.parse(source);
056                List<Weather>weatherList = handler.getWeatherList();
057                TableLayout table = (TableLayout)findViewById(R.id.table);
058                table.removeAllViews();
059                for(Weather weather:weatherList){
060                    TableRow row = new TableRow(this);
061                    row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
062                    row.setGravity(Gravity.CENTER_VERTICAL);
063                    ImageView img = new ImageView(this);
064                    img.setImageDrawable(loadImage(weather.getImageUrl()));
065                    img.setMinimumHeight(80);
066                    row.addView(img);
067                    TextView day = new TextView(this);
068                    day.setText(weather.getDay());
069                    day.setGravity(Gravity.CENTER_HORIZONTAL);
070                    row.addView(day);
071                    TextView temp = new TextView(this);
072                    temp.setText(weather.getLowTemp()+"℃-"+weather.getHighTemp()+"");
073                    temp.setGravity(Gravity.CENTER_HORIZONTAL);
074                    row.addView(temp);
075                    TextView condition = new TextView(this);
076                    condition.setText(weather.getCondition());
077                    condition.setGravity(Gravity.CENTER_HORIZONTAL);
078                    row.addView(condition);
079                    table.addView(row);
080                }
081            } catch (Exception e) {
082                e.printStackTrace();
083                new AlertDialog.Builder(this)
084                    .setTitle("解析错误")
085                    .setMessage("获取天气数据失败,请稍候再试。")
086                    .setNegativeButton("确定"null)
087                    .show();       
088            }
089             
090        }
091        private Drawable loadImage(String imageUrl) {
092            try {
093                return Drawable.createFromStream((InputStream) new URL("http://www.google.com/"+imageUrl).getContent(), "test");
094            } catch (MalformedURLException e) {
095                e.printStackTrace();
096            } catch (IOException e) {
097                e.printStackTrace();
098            }
099            return null;
100        }
101    }
posted on 2011-12-18 21:19 life02 阅读(2235) 评论(0)  编辑 收藏 引用 所属分类: Android开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理