當前位置:首頁 » 安卓系統 » android獲取城市天氣

android獲取城市天氣

發布時間: 2022-09-10 02:00:57

1. android,點擊按妞產生城市和天氣。城市是百度地圖得到的,然後根據城市,解析xml得到天氣預

  1. 你可以打下斷點debug一下,方法運行了沒,就知道哪裡不合適了。

  2. 網路請求里調網路請求,這種邏輯就是不對的,因為可能城市數據還沒有收到,天氣的就執行完了。而此時,沒有城市數據,當然無界。

  3. 正確的是應該設置回調介面,拿到城市數據之後通知,天氣解析方法。

  4. 還可以運用handler發送消息,當城市信息拿到之後通過handler通知天氣解析方法,


注意一定要懂點設計模式,面向對象一些基本思想才去進行功能上方法上的實現。

2. android開發 怎麼顯示天氣

本經驗將介紹Android如何獲取天氣預報主要使用了中國天氣網的介面,使用webView顯示。
工具/原料
Android Studio
方法/步驟
首先我們打開下載安裝好的Android Studio然後新建一個項目,我這里為了方便就直接添加一個Activity了

然後我們添加界面布局代碼,布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<Button
android:id="@+id/bj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bj" />

<Button
android:id="@+id/sh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sh" />

<Button
android:id="@+id/heb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/heb" />

<Button
android:id="@+id/cc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cc" />

<Button
android:id="@+id/sy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sy" />

<Button
android:id="@+id/gz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gz" />

</LinearLayout>
<WebView android:id="@+id/webView1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:focusable="false"
android:layout_weight="1"
/>

</LinearLayout>

然後我們添加後台代碼:
package com.basillee.asus.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity7 extends Activity implements OnClickListener {
private WebView webView; //聲明WebView組件的對象

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity7);
webView=(WebView)findViewById(R.id.webView1); //獲取WebView組件
webView.getSettings().setJavaScriptEnabled(true); //設置JavaScript可用
webView.setWebChromeClient(new WebChromeClient()); //處理JavaScript對話框
webView.setWebViewClient(new WebViewClient()); //處理各種通知和請求事件,如果不使用該句代碼,將使用內置瀏覽器訪問網頁
webView.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm "); //設置默認顯示的天氣預報信息
webView.setInitialScale(57*4); //放網頁內容放大4倍
Button bj=(Button)findViewById(R.id.bj); //獲取布局管理器中添加的「北京」按鈕
bj.setOnClickListener(this);
Button sh=(Button)findViewById(R.id.sh); //獲取布局管理器中添加的「上海」按鈕
sh.setOnClickListener(this);
Button heb=(Button)findViewById(R.id.heb); //獲取布局管理器中添加的「哈爾濱」按鈕
heb.setOnClickListener(this);
Button cc=(Button)findViewById(R.id.cc); //獲取布局管理器中添加的「長春」按鈕
cc.setOnClickListener(this);
Button sy=(Button)findViewById(R.id.sy); //獲取布局管理器中添加的「沈陽」按鈕
sy.setOnClickListener(this);
Button gz=(Button)findViewById(R.id.gz); //獲取布局管理器中添加的「廣州」按鈕
gz.setOnClickListener(this);
}
@Override
public void onClick(View view){
switch(view.getId()){
case R.id.bj: //單擊的是「北京」按鈕
openUrl("101010100T");
break;
case R.id.sh: //單擊的是「上海」按鈕
openUrl("101020100T");
break;
case R.id.heb: //單擊的是「哈爾濱」按鈕
openUrl("101050101T");
break;
case R.id.cc: //單擊的是「長春」按鈕
openUrl("101060101T");
break;
case R.id.sy: //單擊的是「沈陽」按鈕
openUrl("101070101T");
break;
case R.id.gz: //單擊的是「廣州」按鈕
openUrl("101280101T");
break;
}
}
//打開網頁的方法
private void openUrl(String id){
webView.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm?id="+id+" "); //獲取並顯示天氣預報信息
}
}

然後我們點擊Android Studio上面的運行按鈕:

這里要訪問網路我們要添加許可權:
<uses-permission android:name="android.permission.INTERNET" />

6
我們然後可以在模擬器上面可以看到獲取的天氣情況

3. 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" />

4. android 高德2.10如何獲取天氣預報啊

恩,調用谷歌天氣預報的api,返回xml,解析一下顯示在控制項里就可以了!

5. 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":"不易發"
}
}

6. android 做一個天氣預報的步驟

這些問題還是建議你去其他專業的平台去問,那些csdn有很多大神在,他們會詳細專業一點的回答道你,我的水平也就只是做過課程設計的而已,一般的步驟大概是規劃好基礎模型(就是要做出的基本功能)--做好框架控制項那些(軟體的話,我當時是用AS來做的)--然後就實現頁面的跳轉連接--最後就關聯後台數據(這個可以說核心了,畢竟天氣預報就是需要數據,實時更新的那種)

7. android 如何實現獲取天氣預報信息

方法步驟(以安卓5.0為例)

一、打開GPS

九、提示

1.適時刷新天氣需要開啟GPS定位。

2.在沒有WIFI的地方刷新天氣需要消耗一定的流量,刷新頻率越高,消耗流量越多。

熱點內容
電腦怎麼看網路密碼 發布:2025-01-10 14:56:40 瀏覽:108
java調用shell腳本參數 發布:2025-01-10 14:43:51 瀏覽:52
php數組計數 發布:2025-01-10 14:23:03 瀏覽:474
s盒演算法 發布:2025-01-10 14:16:42 瀏覽:643
c語言用二分法求方程 發布:2025-01-10 14:15:45 瀏覽:220
廣場舞加密 發布:2025-01-10 14:13:21 瀏覽:520
網路密碼顯示低安全性是什麼意思 發布:2025-01-10 14:11:49 瀏覽:782
恥辱2博士保險箱密碼是多少 發布:2025-01-10 14:11:41 瀏覽:101
如何把伺服器搭在自己電腦 發布:2025-01-10 14:10:57 瀏覽:585
水晶可以存儲 發布:2025-01-10 14:09:35 瀏覽:391