




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android 小孙中文天气预报程序2010年09月13日 星期一 12:47首先了解下,Google天气预报的API吧。 /ig/api?&weather=城市名(中文全拼).我发现在模拟器上面返回的结果和在pc上返回的结果不太一样,pc上有中文的,android模拟器上没有的。好了不说别的了。now start。1。建立工程。我取名sbsWeather。结构如下:再写布局文件,这里只是简单的布局,如果想做的漂亮,去看下iphone的布局,可以得到启发。2。写布局。1.2. 3. 8. 13. 19. 25. 3。我们要做的是中文天气预报,可是Google为我们返回的是中文。再有我们得把用户输入的中文转换为拼音再传给Google才可以。现在,需要准备一些工具。编写中文拼音转换类,Cn2En.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 .HttpURLConnection; 6. import .MalformedURLException; 7. import .URL; 8. import android.graphics.Bitmap; 9. import android.graphics.BitmapFactory; 10. public class Utils 11. public static String week(String enWeek) 12. if (enWeek.equals(Mon) | enWeek.equals(Monday) 13. return 星期一; 14. else if (enWeek.equals(Tue) | enWeek.equals(Tuesday) 15. return 星期二; 16. else if (enWeek.equals(Wed) | enWeek.equals(Wednesday) 17. return 星期三; 18. else if (enWeek.equals(Thu) | enWeek.equals(Thursday) 19. return 星期四; 20. else if (enWeek.equals(Fri) | enWeek.equals(Friday) 21. return 星期五; 22. else if (enWeek.equals(Sat) | enWeek.equals(Saturday) 23. return 星期六; 24. else if (enWeek.equals(Sun) | enWeek.equals(Sunday) 25. return 星期日; 26. return ; 27. 28. public static String weather(String enWeather) 29. if (enWeather.equals(Clear) 30. return 晴; 31. else if (enWeather.equals(Partly Sunny) 32. | enWeather.equals(partly_cloudy) 33. return 多云; 34. else if (enWeather.equals(Chance of Rain) 35. return 晴转雨; 36. else if (enWeather.equals(storm) 37. return 暴雨; 38. else if (enWeather.equals(thunderstorm) 39. return 雷阵雨; 40. else if (enWeather.equals(fog) 41. return 大雾; 42. else if (enWeather.equals(haze) 43. return 有雾; 44. else if (enWeather.equals(rain) 45. return 雨; 46. else if (enWeather.equals(heavyrain) 47. return 大雨; 48. else if (enWeather.equals(lightrain) 49. return 小雨; 50. else if (enWeather.equals(heavyrain) 51. return 大雨; 52. else if (enWeather.equals(snow) 53. return 有雪; 54. / / 还需要补充。 55. return ; 56. 57. public static Bitmap returnBitMap(String imgUrl) 58. URL myImgUrl = null; 59. Bitmap bitmap = null; 60. try 61. myImgUrl = new URL(imgUrl); 62. catch (MalformedURLException e) 63. e.printStackTrace(); 64. 65. try 66. HttpURLConnection conn = (HttpURLConnection) myImgUrl 67. .openConnection(); 68. conn.setDoInput(true); 69. conn.connect(); 70. InputStream is = conn.getInputStream(); 71. bitmap = BitmapFactory.decodeStream(is); 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 onCreate(Bundle savedInstanceState) 41. super.onCreate(savedInstanceState); 42. setContentView(R.layout.main); 43.44. ETplace = (EditText)findViewById(R.id.place); 45. query = (Button)findViewById(R.id.query); 46. imView = (ImageView)findViewById(R.id.myImageView); 47. placeName = (TextView)findViewById(R.id.placeName); 48.49. query.setOnClickListener(new Button.OnClickListener() 50. public void onClick(View v) 51. try 52. TvPlace = (TextView)findViewById(R.id.tvPlace); 53.54. String place = CntoEn.getFullSpell(ETplace.getText().toString(); 55. placeName.setText(place); 56. String weather = ; 57. String url = /ig/api?&weather=+place; 58. DefaultHttpClient client = new DefaultHttpClient(); 59. HttpUriRequest req = new HttpGet(url); 60. HttpResponse resp = client.execute(req); 61.62. /String strResult = EntityUtils.toString(resp.getEntity(); 63. /Log.i(weather-, 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.parse(new InputSource(stream); 72. NodeList n = d.getElementsByTagName(forecast_conditions); 73.74. / 获得图片url 当天的。 75. String imgUrl = ; 76. imgUrl += n.item(0).getChildNodes().item(3).getAttributes().item(0).getNodeValue(); 77. imView.setImageB
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公司组织晚餐活动方案
- 公司梦想活动方案
- 公司春节布置活动方案
- 公司组织旅行活动方案
- 公司活动秋游活动方案
- 公司红酒品鉴活动方案
- 公司欢送仪式活动方案
- 公司系列大讲堂活动方案
- 公司母亲节日活动方案
- 公司水饺比赛活动方案
- 【学校心理健康教育系列】心理韧性:成为更坚韧的自己
- AI技术支持的学情分析
- 核电站sdm手册第7章
- JGJ-130-2011建筑施工扣件式钢管脚手架安全技术规范(新版)
- 鲁东教师心理健康期末考试复习题及参考答案
- 蛋白质纯化技术PPT幻灯片课件
- 企业部门人员需求申请表
- 手太阴肺经ppt课件
- 城市居住区规划设计规范(含条文说明)
- (完整版)《普通心理学-彭聃龄》知识要点
- 借款担保人担保承诺书
评论
0/150
提交评论