`
844604778
  • 浏览: 544973 次
文章分类
社区版块
存档分类
最新评论

天气预报(Android版)

 
阅读更多

今天终于把天气预报给弄出来了,心里小小的高兴一下,虽然这东西网上早就有了,但那毕竟是别人的。在想做这个之前,以为天气预报软件好高深,不知道从何下手。不过现在想想这东西也不是想象中那么复杂。最主要的是自己亲手做出来,感觉挺很爽的,要是有真机就更好了!

OK,在下面我把天气预报简单叫weather好了,在做weather之前我在网上有搜了一下有关它的资料,知道weather一般的是解析XML文档来获取信息。可能还有别的方式Json.... 然后再结合自己学的Android知识就可以完成本次小程序。本次写的weather里主要涉及到AutoCompleteTextView,ListView以及SAX技术 。先上weather运行时的效果图:


11.gif



weather布局很丑,哎,俺没啥艺术细胞,只想到这样的布局,重在功能实现上。这次是用SAX解析XML,同样是用到了谷歌提供的天气接口。在上次的学习SAX解析中里面我做的是解析本地的XML,很顺利。在做weather的同时我顺便写了个控制台版的天气预报,发现了有同样的问题:结果只出现温度,而中文信息却是乱码。在这上面我纠结了大半天后来才知道为什么,原因是编码不对,最后换成了GBK编码就搞定了 下面是代码片段。

  1. URL url = new URL(path);
  2. URLConnection conn = url.openConnection();
  3. InputStreamReader isr = new InputStreamReader(conn.getInputStream(),"GBK"); //GBK编码就OK,用utf-8还是出现乱码
  4. BufferedReader br = new BufferedReader(isr);
  5. InputSource is = new InputSource(br);
  6. 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
  1. package com.weather.manymore13;

  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.util.ArrayList;
  10. import java.util.Map;

  11. import javax.xml.parsers.ParserConfigurationException;
  12. import javax.xml.parsers.SAXParser;
  13. import javax.xml.parsers.SAXParserFactory;

  14. import org.xml.sax.InputSource;
  15. import org.xml.sax.SAXException;
  16. import org.xml.sax.XMLReader;

  17. import android.app.Activity;
  18. import android.content.Intent;
  19. import android.graphics.Bitmap;
  20. import android.graphics.Color;
  21. import android.os.Bundle;
  22. import android.text.StaticLayout;
  23. import android.view.View;
  24. import android.view.View.OnClickListener;
  25. import android.widget.ArrayAdapter;
  26. import android.widget.AutoCompleteTextView;
  27. import android.widget.Button;
  28. import android.widget.ImageView;
  29. import android.widget.ListView;
  30. import android.widget.SimpleAdapter;
  31. import android.widget.SimpleAdapter.ViewBinder;
  32. import android.widget.TextView;

  33. public class WeatherActivity extends Activity {
  34. private AutoCompleteTextView autoText;
  35. private Button btnConfirm;
  36. private TextView tViewCurrent;
  37. private ListView lv;
  38. private ArrayList<Map<String,Object>> list;
  39. private ImageView currentIcon;
  40. private TextView currentInfo;
  41. private TextView futionTime;
  42. private SimpleAdapter simpAdapter = null;
  43. private boolean visit = true;
  44. public void init()
  45. {
  46. autoText = (AutoCompleteTextView)findViewById(R.id.autoTextView);
  47. btnConfirm = (Button)findViewById(R.id.btn_confirm);
  48. tViewCurrent = (TextView)findViewById(R.id.current_time);
  49. lv = (ListView)findViewById(R.id.myListView);
  50. currentIcon = (ImageView)findViewById(R.id.current_icon);
  51. currentInfo = (TextView)findViewById(R.id.current_info);
  52. futionTime = (TextView)findViewById(R.id.future_time);
  53. }

  54. @Override
  55. public void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.main);
  58. init();


  59. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
  60. (this, android.R.layout.simple_dropdown_item_1line,
  61. Tools.city);
  62. autoText.setAdapter(arrayAdapter);
  63. btnConfirm.setOnClickListener(new OnClickListener() {

  64. public void onClick(View arg0) {

  65. System.out.println("按钮被按下");
  66. String city = autoText.getText().toString().trim();

  67. tViewCurrent.setVisibility(View.INVISIBLE);
  68. futionTime.setVisibility(View.INVISIBLE);
  69. if(!(city.equals("")))
  70. {
  71. try {


  72. obtainWeacherInfo(Tools.SEACH_URL+city);
  73. if(simpAdapter!=null)
  74. {
  75. simpAdapter.notifyDataSetChanged();
  76. }


  77. } catch (MalformedURLException e) {
  78. System.out.println("url出错");
  79. e.printStackTrace();
  80. }
  81. catch (Exception e) {
  82. e.printStackTrace();
  83. }
  84. }

  85. }
  86. });


  87. }

  88. public void obtainWeacherInfo(String path) throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException
  89. {

  90. SAXParserFactory parserFactory = SAXParserFactory.newInstance();

  91. SAXParser sp = parserFactory.newSAXParser();

  92. XMLReader xmlReader = sp.getXMLReader();

  93. WeacherHandler handler = new WeacherHandler();

  94. xmlReader.setContentHandler(handler);

  95. URL url = new URL(path);

  96. URLConnection conn = url.openConnection();

  97. InputStreamReader isr = new InputStreamReader(conn.getInputStream(),"GBK");

  98. BufferedReader br = new BufferedReader(isr);

  99. InputSource is = new InputSource(br);

  100. xmlReader.parse(is);

  101. br.close();

  102. displayWeatherInfo(handler);

  103. }


  104. public void displayWeatherInfo(WeacherHandler handler)
  105. {
  106. list = handler.getForecastWeachers();

  107. //显示实时信息
  108. Bitmap bmp = handler.getCurrentWeather().getBmp();
  109. String currentInfoStr = handler.getCurrentWeather().getCurrentInfo().toString();
  110. currentIcon.setImageBitmap(bmp);
  111. currentInfo.setText(currentInfoStr);

  112. if(list.size() < 1)
  113. {

  114. System.out.println("displayWeatherInfo: list.size() < 1");

  115. currentInfo.setText(R.string.prompt);

  116. currentInfo.setTextColor(Color.YELLOW);

  117. currentInfo.setVisibility(View.VISIBLE);


  118. }

  119. // 显示未来天气, 我们这里是在ListView中显示
  120. String[] itemName = new String[]{"week","temperature","condition","icon"};
  121. int[] itemId = new int[]{R.id.week, R.id.temperature, R.id.condition,R.id.icon};
  122. simpAdapter = new SimpleAdapter(this,
  123. list,R.layout.list_item,
  124. itemName,itemId);
  125. lv.setAdapter(simpAdapter);
  126. if(list.size() > 0)
  127. {
  128. tViewCurrent.setVisibility(View.VISIBLE);
  129. futionTime.setVisibility(View.VISIBLE);
  130. }
  131. // 注意在这里用到了绑定数据 ImageView绑定了网络图片
  132. simpAdapter.setViewBinder(new ViewBinder() {

  133. public boolean setViewValue(View view, Object data,
  134. String textRepresentation) {
  135. if(view instanceof ImageView&& data instanceof Bitmap){
  136. ImageView iv = (ImageView) view;
  137. iv.setImageBitmap((Bitmap) data);
  138. return true;
  139. }else {
  140. return false;
  141. }

  142. }

  143. });
  144. }
  145. }
复制代码
在这个里面跟网络地址建立连接,需要得到InputStream流,在这种情况下一般得另开一个线程,这都是耗时的操作,不然有的时候网路出现延迟就会导致程序假死在哪里就不好使了,在这个weather里我没有用线程,随着一步一步的学习后面再加上,最后 ,别忘了加这玩意儿 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
这东西让我蛋疼了很久,升级后,模拟器当时连个错都不爆,千万不要忘记。
分享到:
评论

相关推荐

    天气预报 android版

    主要用到了ListView AutoCompleteTextView SAX技术, 程序截图见 http://blog.csdn.net/manymore13/article/details/6957795

    安卓期末大作业Android天气预报app源码.zip

    安卓期末大作业Android天气预报app源码安卓期末大作业Android天气预报app源码安卓期末大作业Android天气预报app源码安卓期末大作业Android天气预报app源码安卓期末大作业Android天气预报app源码安卓期末大作业...

    Android Studio实现天气预报APP系统源码(也可做Android本科毕业设计).zip

    Android Studio实现天气预报APP系统源码(也可做Android本科毕业设计Android Studio实现天气预报APP系统源码(也可做Android本科毕业设计Android Studio实现天气预报APP系统源码(也可做Android本科毕业设计Android ...

    Google天气预报android版

    每四小时自动更新;可选城市;sax解析本地资源;apache网络下载;widget同步更新显示;

    Android Studio实现天气预报小程序

    Android Studio实现天气预报小程序

    android天气预报

    android天气预报android天气预报android天气预报android天气预报android天气预报android天气预报

    移动 android studio 天气预报

    移动 android studio 天气预报。 最近买了本书《Android第一行代码》,通篇看了下感觉不错,书本最后有个实战项目酷欧天气,闲来无事就照着敲了一遍代码,主要在请求天气接口和背景优化做了些小改动,现在来记录下。...

    android天气预报程序

    android天气预报程序源码 实现了全国各城市天气信息的获取 可以展示最近六天的天气状况 对于android学习者有很大的帮助

    android简易天气预报源码

    自己做的简易天气预报,输入城市名称即可获取当天天气,是OkHttp和Gson练习的一个小例子

    android 谷歌天气预报

    android 谷歌天气预报 android 谷歌天气预报android 谷歌天气预报

    Android Studio项目《天气预报app》

    Android Studio项目《天气预报app》

    android天气预报项目报告

    android天气预报项目报告,毕业设计报告。 第二章 需求分析 一. 功能需求 在这个综合示例中,有一个显示天气情况的用户界面,可以通过图片和文字显示当前和未来几天的天气状况,包括温度、湿度、风向和雨雪情况等。...

    Android Studio实现天气预报APP系统源码+项目报告(可做Android本科毕业设计)

    Android Studio实现天气预报APP系统源码+项目报告(可做Android本科毕业设计) 本项目实现在 Android 移动设备上查询天气信息,因为是移动设备,所以使用起来非 常的便捷与方便,可以随时随地的查询不同城市的天气,...

    Android开发-天气预报应用实现

    1、 在手机中包含主视图和细节视图,主视图显示连续多天的天气预报简讯,如图表 1 所示,用户在主视图中点击某一天的天气简讯以后,跳出细节视图,显示用户选定 当天天气的详细信息。 2、 在平板中使用Master-detail...

    Android天气预报实验报告

    Android天气预报实验报告模板 public class SetCityActivity extends Activity { //定义的一个自动定位的列表 private ListView gpsView; //定义的一个省份可伸缩性的列表 private ExpandableListView ...

    Android之Web Service实现天气预报查询

    Android之Web Service实现天气预报查询 利用Ksoap实现Web Service功能,大家自己练习的时候需要用到Ksoap2包,下载地址为 http://download.csdn.net/detail/hander_wei/5713765

    Android的天气预报源码

    Android的天气预报源码

    Android本科毕业设计基于Android的天气预报APP系统源码.zip

    Android本科毕业设计基于Android的天气预报开发APP系统源码。显示城市当前的实时天气状况,包括城市名称、城市名片、天气情况、温度高低、星期等信息。界面左边有一列城市列表,显示中国各个城市名称,当用户想知道...

    Android Studio实现天气预报App,满分课设,入门必学

    本次项目主要实现了天气预报功能。通过调用天气预报接口来获得天气数据,用LIstView和GridView来搭建每个界面,将查询的天气信息存储在SQLiteDatabase中。应用主要包括了五大界面,每一个界面上都会有很多组件: - ...

Global site tag (gtag.js) - Google Analytics