雅虎定位查询天气

package <span style="font-family: Arial, Helvetica, sans-serif;">com</span><span style="font-family: Arial, Helvetica, sans-serif;">.launcher;</span>

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.amap.mapapi.location.LocationManagerProxy;
import com.launcher.util.Util;

public class Weather implements LocationListener {
	private LocationManagerProxy locationManager;
	private final int CHECKTIME = 60 * 60 * 1000;
	public static final int WEATHER_CURRT = 0x71;
	public static final int WEATHER_CITY = 0x72;
	public static final int WEATHER_FUTURE = 0x73;
	private Handler mHandler;
	private Context mContext;

	public static final int[] WeatherCode = { R.drawable.weather_thunderstorms, R.drawable.weather_thunderstorms, R.drawable.weather_thunderstorms, R.drawable.weather_thunderstorms,
			R.drawable.weather_thunderstorms, // 1-5
			R.drawable.weather_thunderstorms, R.drawable.weather_thunderstorms, R.drawable.weather_thunderstorms, R.drawable.weather_thunderstorms, R.drawable.weather_thunderstorms, // 6-10
			R.drawable.weather_thunderstorms, R.drawable.weather_thunderstorms, R.drawable.weather_few_showers, R.drawable.weather_snow_showers, R.drawable.weather_few_showers, // 11-15
			R.drawable.weather_snow, R.drawable.weather_snow, R.drawable.weather_snow, R.drawable.weather_snow, R.drawable.weather_haze,// 16-20
			R.drawable.weather_haze, R.drawable.weather_haze, R.drawable.weather_haze, R.drawable.weather_haze, R.drawable.weather_haze,// 21-25
			R.drawable.weather_cloudy, R.drawable.weather_partly_cloudy, R.drawable.weather_cloudy, R.drawable.weather_partly_cloudy, R.drawable.weather_cloudy,// 26-30
			R.drawable.weather_cloudy, R.drawable.weather_sunny, R.drawable.weather_sunny, R.drawable.weather_sunny, R.drawable.weather_sunny,// 31-35
			R.drawable.weather_showers, R.drawable.weather_showers, R.drawable.weather_showers, R.drawable.weather_showers, R.drawable.weather_showers, // 36-40
			R.drawable.weather_showers, R.drawable.weather_showers, R.drawable.weather_showers, R.drawable.weather_showers, R.drawable.weather_showers,// 41-45
			R.drawable.weather_showers, R.drawable.weather_showers, R.drawable.weather_showers };// 46-48

	public Weather(Context mContext, Handler mHandler) {
		this.mHandler = mHandler;
		this.mContext = mContext;
		locationManager = LocationManagerProxy.getInstance(mContext);
		Timer timer = new Timer(true);
		timer.schedule(task, 0, CHECKTIME);
		// enableMyLocation();
	}

	TimerTask task = new TimerTask() {
		public void run() {
			if (Util.isOpenNetwork(mContext)) {
				enableMyLocation();
			} else {

			}
		}
	};

	public boolean enableMyLocation() {
		boolean result = false;
		try {
			// getWeather(null); // /null
			result = true;
			Criteria cri = new Criteria();
			cri.setAccuracy(Criteria.ACCURACY_COARSE);
			cri.setAltitudeRequired(false);
			cri.setBearingRequired(false);
			cri.setCostAllowed(false);
			String bestProvider = locationManager.getBestProvider(cri, true);
			locationManager.requestLocationUpdates(bestProvider, 2000, 10, this);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();

			if (Util.isOpenNetwork(mContext)) {
				mHandler.removeMessages(0x808);
				mHandler.sendEmptyMessageDelayed(0x808, 10 * 1000);
			}

		}
		return result;
	}

	public void disableMyLocation() {
		locationManager.removeUpdates(this);
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null) {
			getWeather(location);
			// disableMyLocation();
		}
	}

	public void onDestroy() {
		if (locationManager != null) {
			locationManager.removeUpdates(this);
			locationManager.destory();
		}
		locationManager = null;
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	private Double geoLat = 22.5438191;
	private Double geoLng = 113.9368752;

	public void getWeather(final Location location) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (location == null) {
					Log.i("Weather", "定位失敗!!!!!!!!");
					return;
				}
				geoLat = location.getLatitude();
				geoLng = location.getLongitude();
				String WeatherUrl = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20("
						+ "select%20woeid%20from%20geo.placefinder%20where%20text%3D%22" + geoLng + "%2C" + geoLat + "%22%20and%20gflags%20%3D%20%22R%22)"
						+ "%20and%20u=%22c%22&format=xml&diagnostics=true&callback=";
				try {
					// XMLReader xmlReader =
					// SAXParserFactory.newInstance().newSAXParser().getXMLReader();
					// ArrayList<HashMap<String, String>> weatherArrayList = new
					// ArrayList<HashMap<String, String>>();
					// GetYahooWeatherSaxTools tools = new
					// GetYahooWeatherSaxTools(weatherArrayList);

					HttpClient httpClient = new DefaultHttpClient();
					HttpGet httpGet = new HttpGet(WeatherUrl);
					HttpResponse response = httpClient.execute(httpGet);
					StatusLine statusLine = response.getStatusLine();
					int statusCode = statusLine.getStatusCode();
					if (statusCode == 200) {
						HttpEntity entity = response.getEntity();
						InputStream content = entity.getContent();

						Message msg = new Message();
						msg.obj = ReadXml(new InputSource(new InputStreamReader(content)));
						msg.what = WEATHER_FUTURE;
						mHandler.sendMessage(msg);

						// xmlReader.setContentHandler(tools);
						// xmlReader.parse(new InputSource(new
						// InputStreamReader(content)));
					} else {
						Log.e("error", "Failed to download file");
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	private ArrayList<HashMap<String, String>> ReadXml(InputSource input) {
		ArrayList<HashMap<String, String>> weatherArrayList = new ArrayList<HashMap<String, String>>();
		try {
			XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
			GetYahooWeatherSaxTools tools = new GetYahooWeatherSaxTools(weatherArrayList);

			xmlReader.setContentHandler(tools);
			xmlReader.parse(input);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return weatherArrayList;
	}

	public class GetYahooWeatherSaxTools extends DefaultHandler {
		private ArrayList<HashMap<String, String>> arrayList;
		private HashMap<String, String> hashMap;
		private int date = 0;
		private String City = "";

		public GetYahooWeatherSaxTools(ArrayList<HashMap<String, String>> arrayList) {
			this.arrayList = arrayList;
		}

		@Override
		public void startDocument() throws SAXException {
		}

		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

			/*
			 * if (qName.equals("yweather:condition")) { currtMap.put("temp",
			 * attributes.getValue("temp")); currtMap.put("text",
			 * attributes.getValue("text")); currtMap.put("code",
			 * attributes.getValue("code")); }
			 */

			if (qName.equals("yweather:location")) {
				City = attributes.getValue("city");
			}

			if (qName.equals("yweather:forecast")) {
				hashMap = new HashMap<String, String>();
				// hashMap.put("day", attributes.getValue("day"));
				hashMap.put("date", attributes.getValue("date"));
				hashMap.put("low", attributes.getValue("low"));
				hashMap.put("high", attributes.getValue("high"));
				hashMap.put("text", attributes.getValue("text"));
				hashMap.put("code", attributes.getValue("code"));
				hashMap.put("city", City);
				if (date == 0) {
					hashMap.put("day", "Today");
				} else if (date == 1) {
					hashMap.put("day", "Tomorrow");
				} else {
					return;
				}
				arrayList.add(hashMap);
				date++;
			}
		}

		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {

		}

		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
		}

		@Override
		public void endDocument() throws SAXException {
		}

	}

}


定位使用百度定位,相关百度API

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值