【Android源码 栏目提醒】:网学会员Android源码 为您提供Android_中文天气预报程序 - 编程语言参考,解决您在Android_中文天气预报程序 - 编程语言学习中工作中的难题,参考学习。
http://hi.baidu.com/einyboy/blog/item/1cb8c9656cb43ffef6365404.html
Android 中文天气预报程序 2010年09月13日 星期一 12:47 大家好由于项目原因好长时间没学
android了。
今天抽出点儿时间做个天气预报程序。
贴出来希望对大家有用。
先说一下Google天气预报的AIP吧。
http://www.google.com/ig/apiweather城市名中文全拼. 我发现在模拟器上面返回的结果和在pc上返回的结果不太一样pc上有中文的
android模拟器上没有的。
好了不说别的了。
开始吧 now start。
1。
建立工程。
我取名sbsWeather。
结构如下 我们一步一步来慢慢完善 2。
我习惯先写布局。
1. 2. 3. 8. 13. 19. 25. 复制代码 3。
我们要做的是中文天气预报哦。
可是Google为我们返回的是中文。
再有我们得把用户输入的中文转换为拼音再传给Google才可以。
现在需要准备一些工具。
编写中文拼音转换类CntoEn.java。
网上好多的。
自己随便找一个就哦了。
这个不贴代码了。
搜Java 中文转音频 即可。
Utils.java 转换Google返回的英文。
。
。
一对一的转换吧好像没别的跟好的办法。
另外其中还有个方法是将网络图片转换为
android的bitmap对象的。
1. 2. package com.sbs.weather 3. import java.io.IOException 4. import java.io.InputStream 5. import java.net.HttpURLConnection 6. import java.net.MalformedURLException 7. import java.net.URL 8. import
android.graphics.Bitmap 9. import
android.graphics.BitmapFactory 10. public class Utils 11. public static String weekString enWeek 12. if enWeek.equalsMon enWeek.equalsMonday 13. return 星期一 14. else if enWeek.equalsTue enWeek.equalsTuesday 15. return 星期二 16. else if enWeek.equalsWed enWeek.equalsWednesday 17. return 星期三 18. else if enWeek.equalsThu enWeek.equalsThursday 19. return 星期四 20. else if enWeek.equalsFri enWeek.equalsFriday 21. return 星期五 22. else if enWeek.equalsSat enWeek.equalsSaturday 23. return 星期六 24. else if enWeek.equalsSun enWeek.equalsSunday 25. return 星期日 26. return 27. 28. public static String weatherString enWeather 29. if enWeather.equalsClear 30. return 晴 31. else if enWeather.equalsPartly Sunny 32. enWeather.equalspartly_cloudy 33. return 多云 34. else if enWeather.equalsChance of Rain 35. return 晴转雨 36. else if enWeather.equalsstorm 37. return 暴雨 38. else if enWeather.equalsthunderstorm 39. return 雷阵雨 40. else if enWeather.equalsfog 41. return 大雾 42. else if enWeather.equalshaze 43. return 有雾 44. else if enWeather.equalsrain 45. return 雨 46. else if enWeather.equalsheavyrain 47. return 大雨 48. else if enWeather.equalslightrain 49. return 小雨 50. else if enWeather.equalsheavyrain 51. return 大雨 52. else if enWeather.equalssnow 53. return 有雪 54. // / 还需要补充。
。
。
。
55. return 56. 57. public static Bitmap returnBitMapString imgUrl 58. URL myImgUrl null 59. Bitmap bitmap null 60. try 61. myImgUrl new URLimgUrl 62. catch MalformedURLException e 63. e.printStackTrace 64. 65. try 66. HttpURLConnection conn HttpURLConnection myImgUrl 67. .openConnection 68. conn.setDoInputtrue 69. conn.connect 70. InputStream is conn.getInputStream 71. bitmap BitmapFactory.decodeStreamis 72. is.close 73. catch IOException e 74. e.printStackTrace 75. 76. return bitmap 77. 78. 复制代码 4。
开始真正的工作咯。
下面的程序通过Google提供的api来获取天气预报信息。
1. package com.sbs.weather 2. 3. import java.io.InputStream 4. 5. import javax.xml.parsers.DocumentBuilder 6. import javax.xml.parsers.DocumentBuilderFactory 7. 8. import org.apache.http.HttpEntity 9. import org.apache.http.HttpResponse 10. import org.apache.http.client.methods.HttpGet 11. import org.apache.http.client.methods.HttpUriRequest 12. import org.apache.http.impl.client.DefaultHttpClient 13. import org.apache.http.util.EntityUtils 14. import org.w3c.dom.Document 15. import org.w3c.dom.NodeList 16. import org.xml.sax.InputSource 17. 18. import
android.app.Activity 19. import
android.graphics.Bitmap 20. import
android.os.Bundle 21. import
android.os.Handler 22. import
android.os.Message 23. import
android.util.Log 24. import
android.view.View 25. import
android.widget.Button 26. import
android.widget.EditText 27. import
android.widget.ImageView 28. import
android.widget.TextView 29. 30. public class Weather extends Activity 31. 32. public EditText ETplace 33. public TextView TvPlace 34. public Button query 35. public TextView placeName 36. public ImageView imView 37. 38. / Called when the activity is first created. / 39. Override 40. public void onCreateBundle savedInstanceState 41. super.onCreatesavedInstanceState 42. setContentViewR.layout.main 43. 44. ETplace EditTextfindViewByIdR.id.place 45. query ButtonfindViewByIdR.id.query 46. imView ImageViewfindViewByIdR.id.myImageView 47. placeName TextViewfindViewByIdR.id.placeName 48. 49. query.setOnClickListenernew Button.OnClickListener 50. public void onClickView v 51. try 52. TvPlace TextViewfindViewByIdR.id.tvPlace 53. 54. String place CntoEn.getFullSpellETplace.getText.toString 55. placeName.setTextplace 56. String weather 57. String url http://www.google.com/ig/apiweatherplace 58. DefaultHttpClient client new DefaultHttpClient 59. HttpUriRequest req new HttpGeturl 60. HttpResponse resp client.executereq 61. 62. //String strResult EntityUtils.toStringresp.getEntity 63. //Log.iweather- strResult 64. //一华氏度等于9/5摄氏度数值32 65. 66. HttpEntity ent resp.getEntity 67. InputStream stream ent.getContent 68. 69. DocumentBuilder b DocumentBuilderFactory.newInstance 70. .newDocumentBuilder 71. Document d b.parsenew InputSourcestream 72. NodeList n d.getElementsByTagNameforecast_conditions 73. 74. // 获得图片url 当天的。
75. String imgUrl http://www.google.com 76. imgUrl n.item0.getChildNodes.item3.getAttributes.item0.getNodeValue 77. imView.setImageBitmapUtils.returnBitMapimgUrl 78. 79. // 今后4天预报 80. for int i 0 i n.getLength i 81. weather Utils.weekn.itemi.getChildNodes.item0 82. .getAttributes.item0.getNodeValue 83. weather 84. weather Integer 85. .parseIntn.itemi.getChildNodes.item1 86. .getAttributes.item0.getNodeValue - 32 5 / 9 87. weather 88. weather Integer 89. .parseIntn.itemi.getChildNodes.item2 90. .getAttributes.item0.getNodeValue - 32 5 / 9 91. weather 92. weather Utils.weathern.itemi.getChildNodes.item4 93. .getAttributes.item0.getNodeValue 94. weather n 95. 96. Log.iparseed weather- weather 97. TvPlace.setTextweather 98. 99. catch Exception e 100. e.printStackTrace 101. 102. 103. 104. 105. 复制代码 5。
嗯这一步相当重要就是添加权限。
要是没有权限一切白搭 1. 2. 6. 7. 8. 10. 11. 12. 13. 14. 15. 16. 17. 复制代码 6。
开始运行咯。
源码在这里 sbsWeather.rar 64.82 KB 喜欢的下载吧。
。
。
嗯呢我是好人 7。
总结可以使用数据库保存用户常用的设置。
比如存5个城市等。
还可以加上触屏拖动切换城市的效果这样子就比较完美了。
当然为了节省流量完全可以使用本地图片而不用网络图片。
总之希望各位朋友尤其是大牛们提出建议帮着完善。
02.png 128.52 KB
上一篇:
Android NDK 开发教程五:文件
下一篇:
7*30m公路预应力混凝土连续梁桥上部结构设计