Android 天气预报入门

移动开发 Android
天气,是与我们生活息息相关每日必不可少的话题,而在android平台上,天气类的应用可谓是软件应用中的重中之重,相信用android手机的朋友应该没几个人会不装天气预报软件的,无论是系统自带还是第三方应用,天气类软件绝对是android手机中必不可少的软件类别之一。

一款优秀的天气播报软件,除了在功能上要达到实时准确预报天气之外,其自带的桌面天气插件还能起到美化桌面的作用,今天小编为大家介绍一下天气预报的简单写法,让您时时刻刻都能够第一时间了解最新的天气动态。

1.Android的天气预报的应用还是挺多的,基于JSON和WebServ的均可。

     首先我们来介绍基于JSON解析的天气预报的开发
2.这需要寻找到合适的数据源。

     这里使用的是http://www.weather.com.cn/(中央气象局)的数据信息。可通过 m.weather.com.cn/data/101010100.html或者www.weather.com.cn/data/cityinfo /101010100.html查看到北京的天气信息。

3.接下来就是对JSON数据的解析

  1. package com.cater.weather; 
  2. import java.io.IOException; 
  3. import org.apache.http.HttpResponse; 
  4. import org.apache.http.HttpStatus; 
  5. import org.apache.http.client.ClientProtocolException; 
  6. import org.apache.http.client.HttpClient; 
  7. import org.apache.http.client.methods.HttpGet; 
  8. import org.apache.http.impl.client.DefaultHttpClient; 
  9. import org.apache.http.util.EntityUtils; 
  10. import org.json.JSONException; 
  11. import org.json.JSONObject; 
  12. import android.app.Activity; 
  13. import android.os.Bundle; 
  14. import android.widget.EditText; 
  15. import android.widget.TextView; 
  16. public class WeatherReportActivity extends Activity 
  17. private final static String url = "http://m.weather.com.cn/data/101010100.html"
  18. private HttpClient httpClient; 
  19. private HttpResponse httpResponse; 
  20. private HttpGet httpGet; 
  21. private EditText editText; 
  22. private TextView textView; 
  23. private String today; 
  24. private String dayofweek; 
  25. private String city; 
  26. private int ftime; 
  27. private String template; 
  28.  
  29. @Override 
  30. public void onCreate(Bundle savedInstanceState) 
  31. super.onCreate(savedInstanceState); 
  32. setContentView(R.layout.main); 
  33. editText = (EditText) findViewById(R.id.editText1); 
  34. textView = (TextView) findViewById(R.id.textView1); 
  35. httpClient = new DefaultHttpClient(); 
  36. parseString(getRequest(url)); 
  37. editText.setText(getRequest(url)); 
  38. textView.setText("城市:" + city + "\n" 
  39. +"温度:" + template + "\n" 
  40. +"星期:" + dayofweek + "\n" 
  41. +"时间:" + ftime + "\n" 
  42. +"日期:" + today + "\n"); 
  43.  
  44. private String getRequest(String uri) 
  45. httpGet = new HttpGet(uri); 
  46. String result = ""; 
  47.  
  48. try 
  49. httpResponse = httpClient.execute(httpGet); 
  50. if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
  51. result = EntityUtils.toString(httpResponse.getEntity()); 
  52. return result; 
  53. catch (ClientProtocolException e) 
  54. e.printStackTrace(); 
  55. catch (IOException e) 
  56. e.printStackTrace(); 
  57. return null
  58. private String parseString(String str) 
  59. try 
  60. JSONObject jsonObject = new JSONObject(str).getJSONObject("weatherinfo"); 
  61. today = jsonObject.getString("date_y"); 
  62. dayofweek = jsonObject.getString("week"); 
  63. city = jsonObject.getString("city"); 
  64. ftime = jsonObject.getInt("fchh"); 
  65. template = jsonObject.getString("temp1"); 
  66. catch (JSONException e) 
  67. e.printStackTrace(); 
  68. return null

一款简单的天气预报就做好了!如果你是一个爱学习的人,并耐心的看完了本文的话,希望本文对你有所帮助!

未命名.jpg

责任编辑:闫佳明 来源: my.eoe.cn
相关推荐

2016-03-14 10:29:38

天气预报各类工具源码

2010-08-13 10:56:58

FlexWebservice

2017-08-01 10:10:32

人工智能智能天气预报

2009-07-07 09:25:08

Linux开发FOSS开发项目

2018-01-29 11:25:37

LinuxASCII 字符天气预报

2020-02-11 20:00:29

开源开源工具天气预报

2009-12-02 15:45:04

PHP抓取天气预报

2022-02-21 11:02:54

5G通信网络天气预报

2012-07-16 13:36:54

交换机数据中心核心交换机气象卫星

2013-04-10 17:59:50

微信公众平台接口开发

2020-01-16 15:13:40

AI预测天气预报

2009-04-17 17:11:18

ASP.NET新浪天气

2015-10-19 17:16:10

天气预报命令行Linux

2009-08-26 16:59:44

2019-10-25 19:42:41

华为

2012-06-18 15:40:32

jQuery

2012-03-13 16:45:09

超级计算机沃森Deep Thunde

2022-02-21 15:07:48

气象学人工智能AI

2013-09-09 10:52:10

2015-05-12 11:19:16

大数据改变天气预报
点赞
收藏

51CTO技术栈公众号