获得当地实时天气

来源:互联网 发布:注册商标品牌知乎 编辑:程序博客网 时间:2024/04/28 15:06

获得天气,参考下面的:

http://www.nohacks.cn/post-35.html


//先用LocationManager获得经纬度latitude = location.getLatitude();------longitude = location.getLongitude();

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
} else {
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
edixx.setText(latitude + "");
ediyy.setText(longitude + "");
}
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 0, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}


//再用经纬度获得城市名字

new AsyncTask1().execute("http://lbs.juhe.cn/api/getaddressbylngb?lngx="+ longitude + "&lngy=" + latitude);

//json解析获得名字----edics.setText(obj4.getString("city"));


//在用城市名字获得城市ID

new AsyncTask2().execute("http://api.k780.com:88/?app=weather.city&format=json");

在onPostExecute里,一个一个城市名字去对比。获得城市ID


//再在AsyncTask2()里的onPostExecute方法中,用城市ID查询

//当天天气;new AsyncTask3().execute("http://api.k780.com:88/?app=weather.today&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&weaid="

+ obj3.optString("cityid"));

//历史天气:“http://api.k780.com:88/?app=weather.history&date=20150720&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&weaid="
+ obj3.optString("cityid"));

//天气预报5~7:“http://api.k780.com:88/?app=weather.future&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&weaid="
+ obj3.optString("cityid"));


天气来源链接:http://www.k780.com/api/weather.city


0 0
原创粉丝点击