天氣預報接口網上一艘一大堆。。
基本是國家天氣預報的和google 啊 雅虎啊等。
給個示例地址:http://blog.21004.com/post/178.html
還有和json在線格式化地址:http://tmxk.org/c/json/
然後就是FastJson 地址:http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=2424946
關於Fastjson是幹什麼的,可以百度,簡單解釋就是把 json字符串封裝爲bean 或者吧bean 封裝成 json 方便快讀填充 。
正文:
獲取天氣,封裝了一個類,根據請求地址返回天氣信息,代碼:
package com.wangyyworks.weatherworks; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import utils.NetUtil; import android.content.Context; import android.util.Log; import android.widget.Toast; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.wangyyworks.weatherworks.R.string; /** * 定時從網絡獲取天氣信息,單例使用。 2013-6-7 21:02:16 測試通過 * * @author wang_xiaohao * */ public class WeatherInfoProvider { private static final String TAG = "WeatherInfoProvider"; public String weatheruri = "http://m.weather.com.cn/data/101010100.html";// 天氣預報的uir private static Context mContext; private String fastjsonkey = "weatherinfo";// 用來設置json返回主句後獲得Object // 然後獲得天氣的bean 不然不能填充數據; private WeatherInfo weatherinfo;// 用來封裝請求返回數據的bean; private WeatherInfoProvider() { }; private static WeatherInfoProvider weatherinfoprovider; /** * 單例模式 * * @param context * 需要傳遞上下文 * @return */ public static WeatherInfoProvider getInstance(Context context) { mContext = context; if (weatherinfoprovider == null) { weatherinfoprovider = new WeatherInfoProvider(); } return weatherinfoprovider; } /** * 根據uri請求獲取天氣信息返回 * * @param uri * 請求天氣的uri 代碼 * @return 返回此uri天氣信息 */ private WeatherInfo getWeatherInfo(String weatheruri) { // 使用nettuils訪問uri 然後封裝返回信息; // 判斷網絡是否正常 if (NetUtil.checkNet(mContext)) { // 訪問網絡,返回獲取後的天氣信息bean WeatherInfo weatherinfo = httpClientConn(weatheruri); // TODO接下來,可以傳遞進行匹配展示 } return null; } /** * 根據需要請求的地址 返回請求信息封裝bean * 使用HttpCient連接到國家天氣網站 信息 地址:http://m.weather.com.cn/data/101010100.html * @param weatheruri * 需要請求城市地址 * @return 返回請求城市天氣信息的bean 如果爲null表示請求失敗 */ protected WeatherInfo httpClientConn(String weatheruri) { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(weatheruri); ResponseHandler<String> responseHandler = new BasicResponseHandler(); try { String content = httpclient.execute(httpget, responseHandler); Log.i(TAG, "連接服務器成功!"); // 設置TextView Log.i(TAG, content); // 開始填充數據 content = content.substring(content.indexOf(":") + 1, content.lastIndexOf("}"));// 截取需要的部分、不包含{"weatherinfo":和結尾的} Log.i(TAG, "測試獲得字符串下面繼續解析" + content); weatherinfo = JSON.parseObject(content, WeatherInfo.class); Log.i(TAG, "weatherinfo tostring" + weatherinfo.toString()); } catch (Exception e) { Log.i(TAG, "連接失敗。。。。。"); e.printStackTrace(); } finally { // 關閉網絡連接 httpclient.getConnectionManager().shutdown(); } return weatherinfo; } }
用到一個工具類,檢查手機是否聯網,沒細研究 ,NetUtil代碼:
1 package utils; 2 3 import android.content.ContentResolver; 4 import android.content.Context; 5 import android.database.Cursor; 6 import android.net.ConnectivityManager; 7 import android.net.NetworkInfo; 8 import android.net.Uri; 9 10 public class NetUtil { 11 private static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn"); 12 13 /** 14 * 獲取當前手機端的聯網方式 一旦檢測到當前用戶使用的是wap方式,設置ip和端口 15 */ 16 public static boolean checkNet(Context context) { 17 boolean wifiConnectivity = isWIFIConnectivity(context); 18 boolean mobileConnectivity = isMobileConnectivity(context); 19 20 if (wifiConnectivity == false && mobileConnectivity == false) { 21 // 需要配置網絡 22 return false; 23 } 24 25 // mobile(APN) 26 if (mobileConnectivity) { 27 // wap方式時端口和ip 28 // 不能寫死:IP是10.0.0.172 端口是80 29 // 一部分手機:IP是010.000.000.172 端口是80 30 // 讀取當親的代理信息 31 readApn(context); 32 } 33 return true; 34 } 35 36 /** 37 * 讀取ip和端口信息 38 * 39 * @param context 40 */ 41 private static void readApn(Context context) { 42 ContentResolver resolver = context.getContentResolver(); 43 Cursor query = resolver.query(PREFERRED_APN_URI, null, null, null, null);// 獲取手機Apn:獲取當前處於激活狀態的APN信息 44 if (query != null && query.moveToFirst()) { 45 GloableParams.PROXY_IP = query.getString(query.getColumnIndex("proxy")); 46 GloableParams.PROXY_PORT = query.getInt(query.getColumnIndex("port")); 47 } 48 49 } 50 51 /** 52 * WIFI是否連接 53 * 54 * @param context 55 * @return 56 */ 57 public static boolean isWIFIConnectivity(Context context) { 58 ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 59 NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 60 if (info != null) 61 return info.isConnected(); 62 return false; 63 } 64 65 /** 66 * 手機APN是否連接 67 * 68 * @param context 69 * @return 70 */ 71 public static boolean isMobileConnectivity(Context context) { 72 ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 73 NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 74 if (info != null) 75 return info.isConnected(); 76 return false; 77 } 78 79 }
基本就些了。
WeatherInfo weatherinfo = httpClientConn(weatheruri);
返回的json數據封裝到了weatherinfo,這個類中全部是i常量,貼出來沒意義,寫這個類時候用了notepad++ 的替換,不然要吐血。。
接下來,構思下數據保存和服務,佈局。
兩天能寫出來?額,加油,騷年。
想獲取一天中的溫度變化,接口不提供,悲催。。
做飯去,餓了。。。。
格式化了下封裝好的結果,看起來舒服多了:
06-07 21:27:24.707: I/WeatherInfoProvider(14257): weatherinfo tostringWeatherInfo [ city=北京 city_en=beijing date_y=2013年6月7日 date= week=星期五 fchh=18 cityid=101010100 temp1=19℃~23℃ temp2=18℃~22℃ temp3=16℃~23℃ temp4=14℃~29℃ temp5=19℃~30℃ temp6=20℃~29℃ tempF1=66.2℉~73.4℉ tempF2=64.4℉~71.6℉ tempF3=60.8℉~73.4℉ tempF4=57.2℉~84.2℉ tempF5=66.2℉~86℉ tempF6=68℉~84.2℉ weather1=陣雨轉雷陣雨 weather2=大雨轉小雨 weather3=陰轉陣雨 weather4=多雲轉晴 weather5=晴 weather6=多雲 img1=3 img2=4 img3=9 img4=7 img5=2 img6=3 img7=1 img8=0 img9=0 img10=99 img11=1 img12=99 img_single=4 img_title1=陣雨 img_title2=雷陣雨 img_title3=大雨 img_title4=小雨 img_title5=陰 img_title6=陣雨 img_title7=多雲 img_title8=晴 img_title9=晴 img_title10=晴 img_title11=多雲 img_title12=多雲 img_title_single=雷陣雨 wind1=微風 wind2=微風 wind3=微風 wind4=微風 wind5=微風 wind6=微風 fx1=微風 fx2=微風 fl1=小於3級 fl2=小於3級 fl3=小於3級 fl4=小於3級 fl5=小於3級 fl6=小於3級 index=較舒適 index_d=建議着薄外套、開衫牛仔衫褲等服裝。年老體弱者應適當添加衣物,宜着夾克衫、薄毛衣等。 index48=較舒適 index48_d=建議着薄外套、開衫牛仔衫褲等服裝。年老體弱者應適當添加衣物,宜着夾克衫、薄毛衣等。 index_uv=最弱 index48_uv=最弱 index_xc=不宜 index_tr=一般 index_co=舒適 st1=21 st2=17 st3=20 st4=16 st5=21 st6=15 index_cl=較不宜 index_ls=不宜 index_ag=極不易發 ]
回見~