首先获取天气需要获取知道地理位置,由于百度提供了天气接口和地理接口所有这次用了百度的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 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");// 返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(24 * 60 * 60 * 1000);// 设置发起定位请求的间隔时间为5000ms option.disableCache(true);// 禁止启用缓存定位 option.setPoiNumber(5); // 最多返回POI个数 option.setPoiDistance(1000); // poi查询距离 option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息 mloation.setLocOption(option); } class Mylocationlistener implements BDLocationListener{ @Override public void onReceiveLocation(BDLocation arg0) { // TODO Auto-generated method stub if(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); } @Override public 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 exception e.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 stub this.city=city; this.handler=handler; this.city = city; // 这里的AK替换成自己申请的百度API KEY,申请地址http://lbsyun.baidu.com/apiconsole/key final 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; @Override public void onFailure(HttpException arg0, String arg1) { // TODO Auto-generated method stub } @Override public void onSuccess(ResponseInfo<String> arg0) { // TODO Auto-generated method stub WheatherEntity 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