androidjson天氣
⑴ android中使用java解析json數據
這個格式使用如下代碼解析
try{
JSONObjectjsonObject=newJSONObject(json);
JSONObjectweatherinfo=jsonObject.getJSONObject("weatherinfo");
System.out.println(weatherinfo.getString("city"));
System.out.println(weatherinfo.getString("cityid"));
System.out.println(weatherinfo.getString("temp"));
System.out.println(weatherinfo.getString("WD"));
}catch(JSONExceptione){
e.printStackTrace();
}
其中第一行代碼JSONObjectjsonObject=newJSONObject(json);//json即為你的字元串
比如現在天氣信息是多個城市的,並非只有北京市。
{"weatherinfo":[{"city":"北京","cityid":"101010100","temp":"4","WD":"東風","WS":"2級","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暫無實況","qy":"1011"},{"city":"天津","cityid":"101010100","temp":"4","WD":"東風","WS":"2級","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暫無實況","qy":"1011"}]}
以下代碼適用。
try{
JSONObjectjsonObject=newJSONObject(json);
JSONArrayjsonArray=jsonObject.getJSONArray("weatherinfo");
for(inti=0;i<jsonArray.length();i++){
JSONObjectobject=jsonArray.getJSONObject(i);
System.out.println(object.getString("city"));
System.out.println(object.getString("cityid"));
System.out.println(object.getString("temp"));
System.out.println(object.getString("WD"));
}
}catch(JSONExceptione){
e.printStackTrace();
}
⑵ Android程序設計思路的問題-------天氣預報
判斷軟體是否為首次運行只要在運行軟體的時候來讀取配置文件,如果不存在配置文件,則是首次運行,讓用戶填寫信息後生成配置文件,如果配置文件已經存在則不是首次運行,直接讀取城市就行了
思路為:
如果配置文件存在:
讀取配置信息,#比如"城市"
否則:
彈出輸入框("第一次會用本軟體請設置您所在城市")
寫入配置文件
沒研究過安卓開發,所以只能在思路上幫助你,語法上你自己琢磨一下吧
⑶ 怎麼在android客戶端解析json數據
這個格式使用如下代碼解析try { JSONObject jsonObject = new JSONObject(json); JSONObject weatherinfo = jsonObject.getJSONObject("weatherinfo"); System.out.println(weatherinfo.getString("city")); System.out.println(weatherinfo.getString("cityid")); System.out.println(weatherinfo.getString("temp")); System.out.println(weatherinfo.getString("WD"));} catch (JSONException e) { e.printStackTrace();} 其中第一行代碼 JSONObject jsonObject = new JSONObject(json);//json 即為你的字元串 比如現在天氣信息是多個城市的,並非只有北京市。{"weatherinfo":[{"city":"北京","cityid":"101010100","temp":"4","WD":"東風","WS":"2級","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暫無實況","qy":"1011"},{"city":"天津","cityid":"101010100","temp":"4","WD":"東風","WS":"2級","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暫無實況","qy":"1011"}]}以下代碼適用。 try { JSONObject jsonObject=new JSONObject(json); JSONArray jsonArray=jsonObject.getJSONArray("weatherinfo"); for (int i=0;i<jsonArray.length();i++){ JSONObject object = jsonArray.getJSONObject(i); System.out.println(object.getString("city")); System.out.println(object.getString("cityid")); System.out.println(object.getString("temp")); System.out.println(object.getString("WD")); }} catch (JSONException e) { e.printStackTrace();}
⑷ android 如何實現獲取天氣預報信息
方法步驟(以安卓5.0為例)
一、打開GPS
九、提示
1.適時刷新天氣需要開啟GPS定位。
2.在沒有WIFI的地方刷新天氣需要消耗一定的流量,刷新頻率越高,消耗流量越多。
⑸ android端免費獲取天氣信息的伺服器介面有么
中國國家氣象局提供了獲取所在城市天氣預報信息介面。通過這個介面,我們就可以獲取天氣信息了。
關於獲取天氣預報的實例參考android學習手冊,裡面有源碼。android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載,圖標上有貝殼
中國國家氣象局天氣預報介面總共提供了三個:
http://www.weather.com.cn/data/sk/101010100.html
http://www.weather.com.cn/data/cityinfo/101010100.html
http://m.weather.com.cn/data/101010100.html
註:上面介面2014.3.4日已不再更新。換成這個:http://m.weather.com.cn/atad/101230201.html。各位看34樓。在此感謝tdwll和黃曉佳cobish。
最詳細的信息來自第三個介面。上面url中的101010100是城市代碼,這里是北京的城市代碼。只需要改變城市代碼,就可以得到所在城市的天氣信息。筆者在福州,所以選擇的城市代碼是福州101230101。
在瀏覽器上輸入url:http://m.weather.com.cn/data/101230101.html得到信息,天氣信息是json的數據格式,數據如下:
{"weatherinfo":{"city":"福州","city_en":"fuzhou","date_y":"2012年5月14日","date":"","week":"星期一","fchh":"08","cityid":"101230101","temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃","tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉","weather1":"陣雨轉中雨","weather2":"中雨轉小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨轉陣雨","weather6":"陣雨轉小雨","img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3","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":"天氣較熱,建議著短裙、短褲、短套裝、T恤等夏季服裝。年老體弱者宜著長袖襯衫和單褲。","index48":"暖","index48_d":"較涼爽,建議著長袖襯衫加單褲等春秋過渡裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。","index_uv":"弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"適宜","index_co":"較不舒適","st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18","index_cl":"較不宜","index_ls":"不太適宜","index_ag":"不易發"}}
我們可以解析json數據去得到自己想用的天氣信息。
天氣信息解釋:
[html] view plain print?
{
"weatherinfo":{
<!--基本信息-->
"city":"福州",
"city_en":"fuzhou",
"date_y":"2012年5月14日",
"date":"",
"week":"星期一",
"fchh":"08",
"cityid":"101230101",
<!--從今天開始到第六天的每天的天氣情況,這里的溫度是攝氏溫度-->
"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃",
<!--從今天開始到第六天的每天的天氣情況,這里的溫度是華氏溫度-->
"tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",
<!--天氣描述-->
"weather1":"陣雨轉中雨","weather2":"中雨轉小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨轉陣雨","weather6":"陣雨轉小雨",
<!--天氣描述圖片序號-->
"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3",
<!--圖片名稱-->
"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":"天氣較熱,建議著短裙、短褲、短套裝、T恤等夏季服裝。年老體弱者宜著長袖襯衫和單褲。",
<!--48小時穿衣指數-->
"index48":"暖","index48_d":"較涼爽,建議著長袖襯衫加單褲等春秋過渡裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。",
<!--紫外線及48小時紫外線-->
"index_uv":"弱","index48_uv":"最弱",
<!--洗車-->
"index_xc":"不宜",
<!--旅遊-->
"index_tr":"適宜",、
<!--舒適指數-->
"index_co":"較不舒適",
"st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",
<!--晨練-->
"index_cl":"較不宜",
<!--晾曬-->
"index_ls":"不太適宜",
<!--過敏-->
"index_ag":"不易發"
}
}
{
"weatherinfo":{
<!-- 基本信息 -->
"city":"福州",
"city_en":"fuzhou",
"date_y":"2012年5月14日",
"date":"",
"week":"星期一",
"fchh":"08",
"cityid":"101230101",
<!-- 從今天開始到第六天的每天的天氣情況,這里的溫度是攝氏溫度 -->
"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃",
<!-- 從今天開始到第六天的每天的天氣情況,這里的溫度是華氏溫度 -->
"tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",
<!-- 天氣描述 -->
"weather1":"陣雨轉中雨","weather2":"中雨轉小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨轉陣雨","weather6":"陣雨轉小雨",
<!-- 天氣描述圖片序號 -->
"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3",
<!-- 圖片名稱 -->
"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":"天氣較熱,建議著短裙、短褲、短套裝、T恤等夏季服裝。年老體弱者宜著長袖襯衫和單褲。",
<!-- 48小時穿衣指數 -->
"index48":"暖","index48_d":"較涼爽,建議著長袖襯衫加單褲等春秋過渡裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。",
<!-- 紫外線及48小時紫外線 -->
"index_uv":"弱","index48_uv":"最弱",
<!-- 洗車 -->
"index_xc":"不宜",
<!-- 旅遊 -->
"index_tr":"適宜",、
<!-- 舒適指數 -->
"index_co":"較不舒適",
"st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",
<!-- 晨練 -->
"index_cl":"較不宜",
<!-- 晾曬 -->
"index_ls":"不太適宜",
<!-- 過敏 -->
"index_ag":"不易發"
}
}
⑹ 編一個android的簡單天氣預報程序大概要用到哪些類都是干什麼的可以簡單說一下么
我可能會用到這幾個類:
天氣數據定義,比如晴天陰天,風向,風力,濕度,溫度等。
網路請求和數據解析,因為功能比較簡單,所以可以考慮放在一個類里。
界面類,簡單的就只有一個activity好了,對應一些簡單的功能,刷新天氣之類的。
⑺ android軟體開發:中國氣象局api介面(顯示4、5天的天氣),能實際有用的,日期也得正確
中國國家氣象局天氣預報介面總共提供了三個:
http://www.weather.com.cn/data/sk/101010100.html
http://www.weather.com.cn/data/cityinfo/101010100.html
http://m.weather.com.cn/data/101010100.html
最詳細的信息來自第三個介面。上面url中的101010100是城市代碼,這里是北京的城市代碼。只需要改變城市代碼,就可以得到所在城市的天氣信息。筆者在福州,所以選擇的城市代碼是福州101230101。
在瀏覽器上輸入url:http://m.weather.com.cn/data/101230101.html得到信息,天氣信息是json的數據格式,數據如
{"weatherinfo":{"city":"福州","city_en":"fuzhou","date_y":"2012年5月14日","date":"","week":"星期一","fchh":"08","cityid":"101230101","temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃","tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉","weather1":"陣雨轉中雨","weather2":"中雨轉小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨轉陣雨","weather6":"陣雨轉小雨","img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3","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":"天氣較熱,建議著短裙、短褲、短套裝、T恤等夏季服裝。年老體弱者宜著長袖襯衫和單褲。","index48":"暖","index48_d":"較涼爽,建議著長袖襯衫加單褲等春秋過渡裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。","index_uv":"弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"適宜","index_co":"較不舒適","st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18","index_cl":"較不宜","index_ls":"不太適宜","index_ag":"不易發"}}[/code]
我們可以解析json數據去得到自己想用的天氣信息。
天氣信息解釋:
{
"weatherinfo":{
<!--基本信息-->
"city":"福州",
"city_en":"fuzhou",
"date_y":"2012年5月14日",
"date":"",
"week":"星期一",
"fchh":"08",
"cityid":"101230101",
<!--從今天開始到第六天的每天的天氣情況,這里的溫度是攝氏溫度-->
"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃",
<!--從今天開始到第六天的每天的天氣情況,這里的溫度是華氏溫度-->
"tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",
<!--天氣描述-->
"weather1":"陣雨轉中雨","weather2":"中雨轉小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨轉陣雨","weather6":"陣雨轉小雨",
<!--天氣描述圖片序號-->
"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3",
<!--圖片名稱-->
"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":"天氣較熱,建議著短裙、短褲、短套裝、T恤等夏季服裝。年老體弱者宜著長袖襯衫和單褲。",
<!--48小時穿衣指數-->
"index48":"暖","index48_d":"較涼爽,建議著長袖襯衫加單褲等春秋過渡裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。",
<!--紫外線及48小時紫外線-->
"index_uv":"弱","index48_uv":"最弱",
<!--洗車-->
"index_xc":"不宜",
<!--旅遊-->
"index_tr":"適宜",、
<!--舒適指數-->
"index_co":"較不舒適",
"st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",
<!--晨練-->
"index_cl":"較不宜",
<!--晾曬-->
"index_ls":"不太適宜",
<!--過敏-->
"index_ag":"不易發"
}
}
⑻ android 獲取天氣 本應該返回json數據 但是返回的是html
這個好像是訪問的URL錯了,
或者在請求時加Accept=application/json
⑼ json解析天氣預報虛擬機可以獲取數據,手機不能獲取是什麼原因
1.「json解析天氣預報」
json只是個數據格式,所以你這種說法就有誤。
建議去看:
【整理】什麼是JSON+如何處理JSON字元串
2.「虛擬機可以獲取數據」
你指的是android虛擬機?
那是因為你虛擬機中的網路,是可用的。
3.「手機不能獲取」
可能是:
(1)手機網路不正常
很可能是此問題。
辦法:
找個手機,是開了wifi,並且網路速度快,穩定,然後再去測試。
(2)天氣預報網站,對於外界訪問其內容,做了一定限制
比如檢測出你是移動端,而禁止你訪問
此種可能性較低。
辦法:
試著添加referer等header去規避此限制。
(此處不給貼地址,請自己用google搜標題,即可找到帖子地址)
⑽ android怎麼獲取實時天氣
准備工作:
1.下載華為能力SDK;http://imax.vmall.com/nj-campus/universityEpDown/toDownPage
2.申請一個應用獲取appId和appkey,待會要用到。
簡單的思路就是先通過網路或者gps獲取到當前位置的經緯度,然後通過sdk查詢溫度,獲取結果。
具體步驟如下:
1.創建工程
把sdk中jar包拖到工程中的libs文件夾下面。
2.主類代碼如下
package com.empty.weatherreport;
import com.empty.weatherreport.WeatherUtil.SCell;
import com.empty.weatherreport.WeatherUtil.SItude;
import com.imax.vmall.sdk.android.common.adapter.ServiceCallback;
import com.imax.vmall.sdk.android.entry.CapabilityService;
import com.imax.vmall.sdk.android.entry.CommonService;
import com.imax.vmall.sdk.android.huawei.weather.WeatherService;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class WeatherActivity extends Activity {
private MyHandler myHandler;
private ProgressDialog mProgressDialog;
private Location mLocation;
private boolean sdkStatus;
//Tool to get weather
/**
* CommonService
*/
private CommonService cs;
/**
* WeatherService
*/
private WeatherService weather;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
sdkStatus=false;
myHandler=new MyHandler();
//初始化業務介面實例
weather = CapabilityService.getWeatherServiceInstance();
//實例化CommonService
cs=CommonService.getInstance();
initSDK();
}
private void initSDK()
{
//應用ID,請去iMAX平台注冊申請
String appId="******";
//應用Key
String appKey="******";
//通過CommonService調用鑒權介面,在調用其它能力前必須保證鑒權初始化成功
cs.init(WeatherActivity.this,appId, appKey, new ServiceCallback() {
public void onError(String arg0) {
// TODO Auto-generated method stub
//設置消息
Message msg = new Message();
msg = new Message();
msg.what = 2;
msg.obj = "SDK initialize failed!";
myHandler.sendMessage(msg);
}
public void onComplete(String arg0) {
// TODO Auto-generated method stub
//設置消息
Message msg = new Message();
msg = new Message();
msg.what = 2;
msg.obj = "SDK initialize success!";
sdkStatus=true;
myHandler.sendMessage(msg);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_weather, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==R.id.menu_settings) Toast.makeText(getApplicationContext(), "Ha", Toast.LENGTH_SHORT).show();
if(item.getItemId()==R.id.menu_weather)
{
if(sdkStatus)
{
/** 彈出一個等待狀態的框 */
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Waiting...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.show();
WeatherThread m = new WeatherThread();
new Thread(m).start();
}
else
Toast.makeText(getApplicationContext(), "SDK not installed", Toast.LENGTH_SHORT).show();
}
return super.onMenuItemSelected(featureId, item);
}
/** 顯示結果 */
private void showResult(String s) {
String tmp[]=s.split("\"");
for(int i=0;i<tmp.length;i++)
Log.i("tmp"+i, tmp[i]);
new AlertDialog.Builder(this) .setTitle("Weather") .setMessage("latitude:"+mLocation.getLatitude()+"\n longitude:"
+mLocation.getLongitude()+"\ntmperature:"+tmp[21]) .show();
}
class MyHandler extends Handler {
public MyHandler() {
}
public MyHandler(Looper L) {
super(L);
}
// 子類必須重寫此方法,接管數據
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.d("MyHandler", "handleMessage......");
/** 顯示結果 */
switch(msg.what)
{
case 1:
Log.i("Error", "case1");
mProgressDialog.dismiss();
showResult((String)msg.obj);
break;
case 2:
Toast.makeText(getApplicationContext(), (String)msg.obj, Toast.LENGTH_SHORT).show();
break;
default:;
}
super.handleMessage(msg);
// 此處可以更新UI
}
}
class WeatherThread implements Runnable {
public void run() {
final Message msg = new Message();
msg.what=1;
try {
mLocation=getLocation(WeatherActivity.this);
weather.getWeather(Double.toString(mLocation.getLongitude()),Double.toString(mLocation.getLatitude()), new ServiceCallback()
{
public void onError(String arg0)
{
//api介面調用錯誤響應
Log.i("Error", "getWeather error:"+arg0);
//設置消息
msg.obj = arg0;
/** 關閉對話框 */
myHandler.sendMessage(msg); // 向Handler發送消息,更新UI
}
public void onComplete(String arg0)
{
//api介面調用成功響應
Log.i("Complete", "getWeather complete:"+arg0);
//設置消息
msg.obj = arg0;
/** 關閉對話框 */
myHandler.sendMessage(msg); // 向Handler發送消息,更新UI
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//Get the Location by GPS or WIFI
public Location getLocation(Context context) {
LocationManager locMan = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location location = locMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return location;
}
}
3.載manifest文件中添加許可權
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />