天气预报(Android版)

今天终于把天气预报给弄出来了,心里小小的高兴一下,虽然这东西网上早就有了,但那毕竟是别人的。在想做这个之前,以为

天气预报软件好高深,不知道从何下手。不过现在想想这东西也不是想象中那么复杂。最主要的是自己亲手做出来,感觉挺很爽

的,要是有真机就更好了!

OK,在下面我把天气预报简单叫weather好了,在做weather之前我在网上有搜了一下有关它的资料,知道weather一般的是

解析XML文档来获取信息。

可能还有别的方式Json....所以就有了之前两篇学习解析XML的学习天气预报Dom解析学习SAX解析 以这两篇学习做铺垫。然

后再结合自己学的Android知识就可以完成本次小程序。本次写的weather里主要涉及到AutoCompleteTextView,ListView以及

SAX技术 。先上weather运行时的效果图:

weather布局很丑,哎,俺没啥艺术细胞,只想到这样的布局,重在功能实现上。这次是用SAX解析XML,同样是用到了谷歌提供

的天气接口。在上次的学习SAX解析中里面我做的是解析本地的XML,很顺利。在做weather的同时我顺便写了个控制台版的天气

预报,发现了有同样的问题:结果只出现温度,而中文信息却是乱码。在这上面我纠结了大半天后来才知道为什么,原因是编码不

对,最后换成了GBK编码就搞定了 下面是代码片段。

URL url = new URL(path); URLConnection conn = url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(),"GBK"); //GBK编码就OK,用utf-8还是出现乱码 BufferedReader br = new BufferedReader(isr); InputSource is = new InputSource(br); xmlReader.parse(is);

另外,ListView用SimpleAdapter加载图片,这个适配器的数据是键值对(Map)形式的,如果map中包含有图片,而这个图片不是在

drawable中存在的,比如网络图片,simpleAdapter本身就不支持的。除非你重写适配器(Adapter),

或者用ViewBinder 具体用法见API文档

public void setViewBinder (SimpleAdapter.ViewBinder viewBinder)

SimpleAdapter的外部数据(external clients)可以使用这个类将值绑定到视图。你应该用这个类绑定值到那些不能直接通过SimpleAdapter支持的视图,或者改变通过SimpleAdapter支持绑定的方法的视图。

也就是说simpleAdapter不能直接支持ImageView,像TextView就直接支持,不用大费周章绑定数据。

下面是主类继承Activity

package com.weather.manymore13; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Map; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Bundle; import android.text.StaticLayout; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.SimpleAdapter.ViewBinder; import android.widget.TextView; public class WeatherActivity extends Activity { private AutoCompleteTextView autoText; private Button btnConfirm; private TextView tViewCurrent; private ListView lv; private ArrayList<Map<String,Object>> list; private ImageView currentIcon; private TextView currentInfo; private TextView futionTime; private SimpleAdapter simpAdapter = null; private boolean visit = true; public void init() { autoText = (AutoCompleteTextView)findViewById(R.id.autoTextView); btnConfirm = (Button)findViewById(R.id.btn_confirm); tViewCurrent = (TextView)findViewById(R.id.current_time); lv = (ListView)findViewById(R.id.myListView); currentIcon = (ImageView)findViewById(R.id.current_icon); currentInfo = (TextView)findViewById(R.id.current_info); futionTime = (TextView)findViewById(R.id.future_time); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_dropdown_item_1line, Tools.city); autoText.setAdapter(arrayAdapter); btnConfirm.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { System.out.println("按钮被按下"); String city = autoText.getText().toString().trim(); tViewCurrent.setVisibility(View.INVISIBLE); futionTime.setVisibility(View.INVISIBLE); if(!(city.equals(""))) { try { obtainWeacherInfo(Tools.SEACH_URL+city); if(simpAdapter!=null) { simpAdapter.notifyDataSetChanged(); } } catch (MalformedURLException e) { System.out.println("url出错"); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } }); } public void obtainWeacherInfo(String path) throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); SAXParser sp = parserFactory.newSAXParser(); XMLReader xmlReader = sp.getXMLReader(); WeacherHandler handler = new WeacherHandler(); xmlReader.setContentHandler(handler); URL url = new URL(path); URLConnection conn = url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(),"GBK"); BufferedReader br = new BufferedReader(isr); InputSource is = new InputSource(br); xmlReader.parse(is); br.close(); displayWeatherInfo(handler); } public void displayWeatherInfo(WeacherHandler handler) { list = handler.getForecastWeachers(); //显示实时信息 Bitmap bmp = handler.getCurrentWeather().getBmp(); String currentInfoStr = handler.getCurrentWeather().getCurrentInfo().toString(); currentIcon.setImageBitmap(bmp); currentInfo.setText(currentInfoStr); if(list.size() < 1) { System.out.println("displayWeatherInfo: list.size() < 1"); currentInfo.setText(R.string.prompt); currentInfo.setTextColor(Color.YELLOW); currentInfo.setVisibility(View.VISIBLE); } // 显示未来天气, 我们这里是在ListView中显示 String[] itemName = new String[]{"week","temperature","condition","icon"}; int[] itemId = new int[]{R.id.week, R.id.temperature, R.id.condition,R.id.icon}; simpAdapter = new SimpleAdapter(this, list,R.layout.list_item, itemName,itemId); lv.setAdapter(simpAdapter); if(list.size() > 0) { tViewCurrent.setVisibility(View.VISIBLE); futionTime.setVisibility(View.VISIBLE); } // 注意在这里用到了绑定数据 ImageView绑定了网络图片 simpAdapter.setViewBinder(new ViewBinder() { public boolean setViewValue(View view, Object data, String textRepresentation) { if(view instanceof ImageView && data instanceof Bitmap){ ImageView iv = (ImageView) view; iv.setImageBitmap((Bitmap) data); return true; }else { return false; } } }); } }

在这个里面跟网络地址建立连接,需要得到InputStream流,在这种情况下一般得另开一个线程,这都是耗时的操作,不然有的时

网路出现延迟就会导致程序假死在哪里就不好使了,在这个weather里我没有用线程,随着一步一步的学习后面再加上,

最后 ,别忘了加这玩意儿 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

这东西让我蛋疼了很久,升级后,模拟器当时连个错都不爆,千万不要忘记。

manymore13的weather源码 上传日期:2011-11-11得意百年一遇的神棍节,俺们太激动了!做这么年光棍在今年依然是光棍,依然坚强着活着!我在这里祝各位棍友神

棍节快乐!棍子棒棒的!

本次记录就到此为止。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值