android 天气预报

android有些应用中需要使用天气预报,来展示今天、未来几天的天气状况,那么具体怎么做呢。
一起来看看

[b]首先需要 国家气象局提供的天气预报接口[/b]

1.风力和湿度: [url]http://www.weather.com.cn/data/sk/101010100.html[/url]

2.气温和天气状况:[url]http://www.weather.com.cn/data/cityinfo/101010100.html[/url]

3.7日气温:[url]http://m.weather.com.cn/data/101010100.html[/url]

后面9位数的是城市编码 101010100 代表北京。

[b]其次 城市代码,访问一个城市地区的天气借口必须要有这个城市地区的代码[/b]
例如:


北京:101010100朝阳:101010300顺义:101010400怀柔:101010500通州:101010600昌平:101010700延庆:101010800丰台:101010900石景山:101011000大兴:101011100房山:101011200密云:101011300门头沟:101011400平谷:101011500八达岭:101011600佛爷顶:101011700汤河口:101011800密云上甸子:101011900斋堂:101012000霞云岭:101012100北京城区:101012200海淀:101010200


可通过一下链接获取省级代码:

获取省级代码:[url]http://www.weather.com.cn/data/list3/city.xml?level=1[/url]


[b]最后 有的接口中还会带有一些图片数据,访问地址为[/b]

http://m.weather.com.cn/img/c0.gif http://m.weather.com.cn/img/b0.gif

http://www.weather.com.cn/m/i/weatherpic/29x20/d0.gif

http://www.weather.com.cn/m2/i/icon_weather/29x20/n00.gif

这个图就是天气现象0(晴)的图片,其他天气现象的图片依此类推。

[b]c打头的图片是20*20像素的,
b打头的是50*46像素的,
d打头的是反白的图标,29*20像素,
n打头的是夜间反白图标,29*20像素[/b]

参考文献:[url]http://g.kehou.com/t1029846752.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Android天气预报的基本实现步骤: 1.创建一个Android项目,并在布局文件中添加一个TextView用于显示天气信息,以及一个Button用于刷新天气信息。 2.在MainActivity中,使用OkHttp库向Tomcat服务器发送HTTP请求,获取天气信息的JSON数据。 3.解析JSON数据,获取需要的天气信息。 4.将天气信息显示在TextView中。 5.在Button的点击事件中,重新发送HTTP请求,获取最新的天气信息,并更新TextView中的内容。 以下是示例代码: ```java public class MainActivity extends AppCompatActivity { private TextView mWeatherTextView; private Button mRefreshButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWeatherTextView = findViewById(R.id.weather_text_view); mRefreshButton = findViewById(R.id.refresh_button); mRefreshButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { refreshWeather(); } }); // 显示默认天气信息 showDefaultWeather(); } private void showDefaultWeather() { mWeatherTextView.setText("默认天气信息"); } private void refreshWeather() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("http://your-tomcat-server/weather.json") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { String json = response.body().string(); final String weather = parseWeather(json); runOnUiThread(new Runnable() { @Override public void run() { mWeatherTextView.setText(weather); } }); } }); } private String parseWeather(String json) { // 解析JSON数据,获取天气信息 return "天气信息"; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值