• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            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 閱讀(2242) 評論(0)  編輯 收藏 引用 所屬分類: Android開發
            久久精品国产免费| 91精品国产色综合久久| 精品国产91久久久久久久a | 国产精品亚洲综合久久| 伊人色综合久久天天人守人婷 | 中文字幕人妻色偷偷久久| 99久久99久久久精品齐齐| 亚洲嫩草影院久久精品| 日韩精品久久无码中文字幕| 久久久WWW成人免费精品| 亚洲国产另类久久久精品| 久久久久噜噜噜亚洲熟女综合| 久久综合久久综合亚洲| 91精品国产91久久久久久蜜臀| 亚洲精品无码久久毛片| 青青草国产成人久久91网| 国产精品9999久久久久| 人妻无码久久一区二区三区免费 | 97久久超碰成人精品网站| 欧美午夜A∨大片久久| 久久免费视频观看| 伊人色综合久久天天人手人婷| 热久久国产精品| 国内精品久久久久久99蜜桃| 久久成人小视频| 思思久久好好热精品国产| 国产精自产拍久久久久久蜜| 国产精品永久久久久久久久久| 久久久久成人精品无码中文字幕 | 婷婷久久综合九色综合98| 久久久久无码精品国产不卡| 一本久久a久久精品亚洲| 久久精品国产一区二区三区不卡| 久久国产乱子伦免费精品| 热re99久久6国产精品免费| 97精品伊人久久大香线蕉| 手机看片久久高清国产日韩| 久久久艹| 欧美日韩久久中文字幕| 免费精品久久天干天干| 99精品国产99久久久久久97|