android制作一个简单的天气预报
2015-04-16 16:28
567 查看
最近由于要做一个关于天气的小应用,所以不得不费心上网开始搜索解决方案,找了几个网上疯传的免费的HTTP的接口,经过试验得到的数据都不是最新,发现还是去年的数据,最终找到一个百度地图接供的API的接口,但是首先需要获取一个开发者的密钥才行,所以第一步去注册得到一个密钥,然后是阅读该API的使用说明。
废话不多说,上API的说明:
其中我用到JSON对象来处理,直接上JAVA代码:
由于试验先在模拟器运行了一把:
看结果基本是能用的,至于想做的更好的话,就是加上一些天气的图片,或者是用
该API提供返回的图片资源,根据自己的需求去尽情发挥吧!
废话不多说,上API的说明:
接口说明 根据经纬度/城市名查询天气的结果 接口示例 http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey 接口参数说明 参数类型 参数名称 是否必须 具体描述 String ak true 开发者密钥 String sn false 若用户所用ak的校验方式为sn校验时该参数必须。 (sn生成算法) String location true 输入城市名或经纬度,城市名称如:北京,经纬度格式为lng,lat坐标如: location=116.305145,39.982368;城市天气预报中间"|"分隔,location=116.305145,39.982368| 122.305145,36.982368|…. String output false 输出的数据格式,默认为xml格式,当output设置为’json’时,输出的为json格式的数据; String coord_type false 请求参数坐标类型,默认为gcj02经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托坐标,gcj02表示经过国测局加密的坐标。wgs84表示gps获取的坐标。 返回结果 参数名称 含义 说明 currentCity 当前城市 返回城市名 status 返回结果状态信息 date 当前时间 年-月-日 results 天气预报信息 白天可返回近期3天的天气情况(今天、明天、后天)、晚上可返回近期4天的天气情况(今天、明天、后天、大后天) results.currentCity 当前城市 results.weather_data weather_data.date 天气预报时间 weather_data.dayPictureUrl 白天的天气预报图片url weather_data.nightPictureUrl 晚上的天气预报图片url weather_data.weather 天气状况 所有天气情况(”|”分隔符):晴|多云|阴|阵雨|雷阵雨|雷阵雨伴有冰雹|雨夹雪|小雨|中雨|大雨|暴雨|大暴雨|特大暴雨|阵雪|小雪|中雪|大雪|暴雪|雾|冻雨|沙尘暴|小雨转中雨|中雨转大雨|大雨转暴雨|暴雨转大暴雨|大暴雨转特大暴雨|小雪转中雪|中雪转大雪|大雪转暴雪|浮尘|扬沙|强沙尘暴|霾 weather_data.wind 风力 weather_data.temperature 温度 返回xml格式的数据 <CityWeatherResponse> <status>success</status> <date>2013-07-18</date> <results> <currentCity>北京市</currentCity> <weather_data> <date>周三(今天, 实时:24℃)</date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather>多云</weather> <wind>微风</wind> <temperature>23℃~ 15℃</temperature> <date>周四</date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/leizhenyu.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/zhongyu.png </nightPictureUrl> <weather>雷阵雨转中雨</weather> <wind>微风</wind> <temperature>29~22℃</temperature> <date>周五</date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/yin.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather>阴转多云</weather> <wind>微风</wind> <temperature>31~23℃</temperature> <date>周六</date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather>多云</weather> <wind>微风</wind> <temperature>31~24℃</temperature> </weather_data> <currentCity>合肥市</currentCity> <weather_data> <date>周三</date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather>多云</weather> <wind>东风3-4级</wind> <temperature>27℃</temperature> <date>周四</date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather>多云</weather> <wind>东北风3-4级</wind> <temperature>35~27℃</temperature> <date>周五</date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather>多云</weather> <wind>南风</wind> <temperature>35~27℃</temperature> <date>周六</date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather>多云</weather> <wind>东风</wind> <temperature>34~27℃</temperature> </weather_data> </results> </CityWeatherResponse> <weather>阵雨</weather> <wind>东北风3-4级</wind> <temperature>18℃</temperature> </result> </results> </CityWeatherResponse> 返回json格式的数据 { error: 0, status: "success", date: "2013-07-17", results: [ { currentCity: "北京市", weather_data: [ { date: "周三(今天, 实时:24℃)", dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png", nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png", weather: "多云", wind: "微风", temperature: "23℃~ 15℃" }, { date: "明天(周四)", dayPictureUrl: "http://api.map.baidu.com/images/weather/day/leizhenyu.png", nightPictureUrl: "http://api.map.baidu.com/images/weather/night/zhongyu.png", weather: "雷阵雨转中雨", wind: "微风", temperature: "29~22℃" }, { date: "后天(周五)", dayPictureUrl: "http://api.map.baidu.com/images/weather/day/yin.png", nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png", weather: "阴转多云", wind: "微风", temperature: "31~23℃" }, { date: "大后天(周六)", dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png", nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png", weather: "多云", wind: "微风", temperature: "31~24℃" } ] }, { currentCity: "合肥市", weather_data: [ { date: "今天(周三)", dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png", nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png", weather: "多云", wind: "东风3-4级", temperature: "27℃" }, { date: "明天(周四)", dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png", nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png", weather: "多云", wind: "东北风3-4级", temperature: "35~27℃" }, { date: "后天(周五)", dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png", nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png", weather: "多云", wind: "南风", temperature: "35~27℃" }, { date: "大后天(周六)", dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png", nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png", weather: "多云", wind: "东风", temperature: "34~27℃" } ] } ] }
其中我用到JSON对象来处理,直接上JAVA代码:
package com.example.textview1; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private static final int MSG_WEATHERCHG = 1; private TextView txt_weather_city; private TextView txt_weather_temp; private TextView txt_weather_detail; private TextView txt_weather_date; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt_weather_city = (TextView) findViewById(R.id.text_city); txt_weather_temp = (TextView) findViewById(R.id.text_temp); txt_weather_detail = (TextView) findViewById(R.id.text_weather); txt_weather_date = (TextView) findViewById(R.id.text_date); weatherThread.start(); } public String getWebContent(String url) { // create http request. HttpGet request = new HttpGet(url); // create HttpParams. HttpParams params = new BasicHttpParams(); // set timeout params. HttpConnectionParams.setConnectionTimeout(params, 3000); HttpConnectionParams.setSoTimeout(params, 5000); // create httpclient object. HttpClient httpClient = new DefaultHttpClient(params); try { // get request response. HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { //return content. String content = EntityUtils.toString(response.getEntity()); return content; } else { //warning connect fail. Toast.makeText(getApplicationContext(), getResources().getString( R.string.connected_fails), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } return null; } public Thread weatherThread = new Thread() { @Override public void run() { String weatherUrl = "http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=k68xO83yR6Vx33sdgrcs1"; String weatherJson = getWebContent(weatherUrl); Log.i("TAG", weatherJson); try { JSONObject jsonObject = new JSONObject(weatherJson); String date = jsonObject.getString("date");//date. JSONArray resultArray = jsonObject.getJSONArray("results"); JSONObject resultsObj = resultArray.getJSONObject(0); String currentcity = resultsObj.getString("currentCity");//current cityname. JSONArray weatherArray = resultsObj.getJSONArray("weather_data"); JSONObject weatherObj = weatherArray.getJSONObject(0); WeatherInfo weatherinfo = new WeatherInfo(); weatherinfo.sdate = date; weatherinfo.scity = currentcity; weatherinfo.weather = weatherObj; Message message = new Message(); message.obj = weatherinfo; message.what = MSG_WEATHERCHG; weatherhandler.sendMessage(message); Thread.sleep(3000); } catch (JSONException e) { e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; class WeatherInfo{ public String sdate; public String scity; public JSONObject weather; } private Handler weatherhandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case MSG_WEATHERCHG: { WeatherInfo object = (WeatherInfo) msg.obj; try { txt_weather_city.setText(object.scity); txt_weather_temp.setText(object.weather.getString("temperature")); txt_weather_detail.setText(object.weather.getString("weather")); txt_weather_date.setText(object.sdate); } catch (Exception e) { e.printStackTrace(); } } break; default: break; } } }; }android的XML布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <TextView android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:text="城市名:" android:textSize="16sp" android:textColor="#ffffff"/> <TextView android:id="@+id/text_city" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:layout_toRightOf="@+id/city" android:layout_marginLeft="20dp" android:text="city" android:textSize="16sp" android:textColor="#ffffff"/> <TextView android:id="@+id/temp" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:layout_below="@+id/city" android:text="温 度:" android:textSize="16sp" android:textColor="#ffffff"/> <TextView android:id="@+id/text_temp" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:layout_toRightOf="@+id/temp" android:layout_alignLeft="@+id/text_city" android:layout_below="@+id/text_city" android:text="temperature" android:textSize="16sp" android:textColor="#ffffff"/> <TextView android:id="@+id/weather" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:layout_below="@+id/temp" android:text="天 气:" android:textSize="16sp" android:textColor="#ffffff"/> <TextView android:id="@+id/text_weather" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_toRightOf="@+id/weather" android:layout_alignLeft="@+id/text_temp" android:layout_below="@+id/text_temp" android:gravity="center" android:text="weather" android:textColor="#ffffff" android:textSize="16sp" /> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/weather" android:layout_marginTop="17dp" android:text="时 间:" android:textColor="#ffffff" android:textSize="16sp"/> <TextView android:id="@+id/text_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/date" android:layout_alignBottom="@+id/date" android:layout_alignLeft="@+id/text_weather" android:text="time" android:textColor="#ffffff" android:textSize="16sp" /> </RelativeLayout>
由于试验先在模拟器运行了一把:
看结果基本是能用的,至于想做的更好的话,就是加上一些天气的图片,或者是用
该API提供返回的图片资源,根据自己的需求去尽情发挥吧!
相关文章推荐
- Android上实现一个简单的天气预报APP(二) 配置布局
- Android上实现一个简单的天气预报APP(十) 城市列表搜索框
- Android上实现一个简单的天气预报APP(十一) Preference存储最近一次查询的城市
- Android上实现一个简单的天气预报APP(三) 获取网络数据
- Android上实现一个简单的天气预报APP(十二) 未来三天的天气预报
- Android上实现一个简单的天气预报APP(十三) 导航ViewPager
- Android上实现一个简单的天气预报APP(十四) 使用百度API定位城市
- Android如何制作一个简单的登陆界面
- Android上实现一个简单的天气预报APP(四) 添加按钮点击响应
- Android上实现一个简单的天气预报APP(十五) 发布天气预报APP
- Android上实现一个简单的天气预报APP(五) 解析XML
- Android上实现一个简单的天气预报APP(六) 更新界面数据
- Android 多媒体开发学习之制作一个简单的画板
- 制作一个简单的Android版的音乐播放器
- Android上实现一个简单的天气预报APP(七) 切换到新的界面(选择城市界面)
- android如何制作出一个简单的聊天app
- Android开发——Fragment简单示例(二)制作一个应用菜单导航栏
- Android上实现一个简单的天气预报APP(八) 从数据库读取城市数据
- android制作一个简单登入界面的部分代码
- Android上实现一个简单的天气预报APP(九) ListView展示城市列表、单击更新天气数据