Android 获取天气http://write.blog.csdn.net/category

来源:互联网 发布:张博士医考网络课试听 编辑:程序博客网 时间:2024/05/18 02:03

首先获取天气需要获取知道地理位置,由于百度提供了天气接口和地理接口所有这次用了百度的SDK。首先申请百度开发者,获取KEY,配置AndroidMainfest.xml

相关权限

    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <!-- 百度定位需要的权限 -->    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />    <uses-permission android:name="android.permission.READ_LOGS" />

配置百度service

        <service            android:name="com.baidu.location.f"            android:enabled="true"            android:process=":remote" >        </service>

获取地理位置

public class SelectCity {private LocationClient mloation=null;private BDLocationListener myListener =new Mylocationlistener();private String add;//private Context context;private Handler handler;public SelectCity(Context context,Handler handler) {// TODO Auto-generated constructor stub//// 声明LocationClient类//this.context=context;this.handler=handler;Log.d("run", "select city");mloation = new LocationClient(context);// 注册监听函数mloation.registerLocationListener(myListener);setLocationOption();}public String getadd(){return this.add;}/** * 请求位置信息 */public void requestLocation() {if (mloation.isStarted() == false) {mloation.start();} else {mloation.requestLocation();}}private void setLocationOption() {LocationClientOption option = new LocationClientOption();option.setOpenGps(true);option.setAddrType("all");// 返回的定位结果包含地址信息option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02option.setScanSpan(24 * 60 * 60 * 1000);// 设置发起定位请求的间隔时间为5000msoption.disableCache(true);// 禁止启用缓存定位option.setPoiNumber(5); // 最多返回POI个数option.setPoiDistance(1000); // poi查询距离option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息mloation.setLocOption(option);}classMylocationlistener implements BDLocationListener{@Overridepublic void onReceiveLocation(BDLocation arg0) {// TODO Auto-generated method stubif(arg0==null)return;String addr=arg0.getAddrStr();add=formatCity(addr);UpdateWeather uw=new UpdateWeather(add, handler);Message msg=new Message();msg.what=WheatherEntity.GET;WheatherEntity we=new WheatherEntity();we.setCITY(add);msg.obj=we;handler.sendMessage(msg);Log.d("run", add);}@Overridepublic void onReceivePoi(BDLocation arg0) {// TODO Auto-generated method stub}}/** * 将位置信息转换为城市 *  * @param addr *            位置 * @return 城市名称 */private String formatCity(String addr) {String city ="深圳";try {if (addr.contains("北京市") && addr.contains("区")) {city = addr.substring(addr.indexOf("市") + 1, addr.indexOf("区"));} else if (addr.contains("县")) {city = addr.substring(addr.indexOf("市") + 1, addr.indexOf("县"));} else {int start = addr.indexOf("市");int end = addr.lastIndexOf("市");if (start == end) {if (addr.contains("省")) {city = addr.substring(addr.indexOf("省") + 1,addr.indexOf("市"));} else if (addr.contains("市")) {city = addr.substring(0, addr.indexOf("市"));}} else {city = addr.substring(addr.indexOf("市") + 1,addr.lastIndexOf("市"));}}return city;} catch (Exception e) {// TODO: handle exceptione.printStackTrace();return city;}}}

获取天气

public class UpdateWeather {private String city;private Handler handler;/* * ex. http://api.map.baidu.com/telematics/v3/weather? * location=%E5%8C%97%E4%BA%AC& * output=xml& * ak=zEbewb0YcgBKgY3myDK585Qi *  */public <T> UpdateWeather(String city,final Handler handler) {// TODO Auto-generated constructor stubthis.city=city;this.handler=handler;this.city = city;// 这里的AK替换成自己申请的百度API KEY,申请地址http://lbsyun.baidu.com/apiconsole/keyfinal String ak = "ak=zEbewb0YcgBKgY3myDK585Qi";String location ="location="+city;String output="&output=xml&";String uri = "http://api.map.baidu.com/telematics/v3/weather?"+location+output+ak;Log.d("run", uri);HttpUtils http=new HttpUtils();http.send(HttpMethod.GET, uri, new RequestCallBack<String>() {int i=0;@Overridepublic void onFailure(HttpException arg0, String arg1) {// TODO Auto-generated method stub}@Overridepublic void onSuccess(ResponseInfo<String> arg0) {// TODO Auto-generated method stubWheatherEntity we=new WheatherEntity();String res=arg0.result;//Log.d("run", res);String[] imageurl=res.split("<dayPictureUrl>");String[] imageurl2=imageurl[1].split("</dayPictureUrl>");String[] wind=res.split("<weather>");String[] temperature=res.split("<temperature>");i++;String[] wea=temperature[1].split("</temperature>");String[] wid=wind[1].split("</weather>");we.setwheather(wea[0]);we.setWind(wid[0]);we.setimageurl(imageurl2[0]);Message msg=new Message();msg.what=WheatherEntity.GetWh;msg.obj=we;handler.sendMessage(msg);}});}}


在网上随便找了个资源,调试成功,发给大家

http://download.csdn.net/detail/qq296571277/8964005


0 0
原创粉丝点击