当前位置:首页 » 安卓系统 » 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的地方刷新天气需要消耗一定的流量,刷新频率越高,消耗流量越多。

热点内容
在配置更新的时候没电关机怎么办 发布:2024-05-18 20:36:10 浏览:926
win7访问win2000 发布:2024-05-18 20:27:41 浏览:387
青岛人社局密码多少 发布:2024-05-18 20:19:10 浏览:733
无法存储呼叫转移 发布:2024-05-18 20:18:30 浏览:125
数据库的调优 发布:2024-05-18 20:18:29 浏览:345
sqlserver注册表清理 发布:2024-05-18 20:13:14 浏览:990
linux删除连接 发布:2024-05-18 20:06:56 浏览:821
linux搭建云服务器平台 发布:2024-05-18 19:52:21 浏览:401
安卓怎么关闭美易订阅 发布:2024-05-18 19:29:16 浏览:643
苹果手机配置代理服务器怎么开 发布:2024-05-18 19:29:07 浏览:230