自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android 天氣預報入門

移動開發(fā) Android
天氣,是與我們生活息息相關每日必不可少的話題,而在android平臺上,天氣類的應用可謂是軟件應用中的重中之重,相信用android手機的朋友應該沒幾個人會不裝天氣預報軟件的,無論是系統自帶還是第三方應用,天氣類軟件絕對是android手機中必不可少的軟件類別之一。

一款優(yōu)秀的天氣播報軟件,除了在功能上要達到實時準確預報天氣之外,其自帶的桌面天氣插件還能起到美化桌面的作用,今天小編為大家介紹一下天氣預報的簡單寫法,讓您時時刻刻都能夠第一時間了解最新的天氣動態(tài)。

1.Android的天氣預報的應用還是挺多的,基于JSON和WebServ的均可。

     首先我們來介紹基于JSON解析的天氣預報的開發(fā)
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開發(fā)FOSS開發(fā)項目

2009-12-02 15:45:04

PHP抓取天氣預報

2022-02-21 11:02:54

5G通信網絡天氣預報

2018-01-29 11:25:37

LinuxASCII 字符天氣預報

2020-02-11 20:00:29

開源開源工具天氣預報

2012-07-16 13:36:54

交換機數據中心核心交換機氣象衛(wèi)星

2013-04-10 17:59:50

微信公眾平臺接口開發(fā)

2015-10-19 17:16:10

天氣預報命令行Linux

2009-08-26 16:59:44

Web Service

2009-04-17 17:11:18

ASP.NET新浪天氣

2020-01-16 15:13:40

AI預測天氣預報

2013-09-09 10:52:10

2022-02-21 15:07:48

氣象學人工智能AI

2012-03-13 16:45:09

超級計算機沃森Deep Thunde

2012-06-18 15:40:32

jQuery

2019-10-25 19:42:41

華為

2015-05-12 11:19:16

大數據改變天氣預報
點贊
收藏

51CTO技術棧公眾號