通过google接口在Android中实现天气预报效果Android可以通过google实现获取指定经纬度位置或者某一个城市的天气信息。
如果是根据经纬度查询天气信息,需要对精度为进行转换,例如lat值为31.174165,需要过滤掉小数点,变为31174165传到接口中,维度也一样处理,处理后传给/ig/api?weather=,,,31174165,121433841既可以获取数据。
这里要注意一个问题,如果大家获取的经纬度序列很长,直接去掉小数点,有时候也无法获取天气信息,例如40.478224838152528,124.97828006744385,去掉小数点后,传到参数位置,无法获取值,需要大家将经纬度按下面方式转换一下,只取小数点后6位就可以了。
int latI = (int) (lat * 1E6);int lonI = (int) (lon * 1E6);下面的例子演示了根据输入城市,获取该城市的天气预报,Weather.java的61行,是根据经纬度获取天气信息。
工程结构:Weather.java类package com.AndroidWeather;import java.io.InputStream;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils;import org.w3c.dom.Document;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;import android.app.Activity;import android.graphics.Bitmap;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.TextView;public class Weather extends Activity {public EditText ETplace;public TextView TvPlace;public Button query;public TextView placeName;public ImageView imView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main1);ETplace = (EditText) findViewById(R.id.place);query = (Button) findViewById(R.id.query);imView = (ImageView) findViewById(R.id.myImageView);placeName = (TextView) findViewById(Place);query.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {try {TvPlace = (TextView)findViewById(Place);// String place =ETplace.getText().toString();String place =CntoSpell.getFullSpell(ETplace.getText().toString());placeName.setText(place);String weather = "";// String url ="/ig/api?&weather="// + place;String url ="/ig/api?weather=,,,31174165,121433841";DefaultHttpClient client = new DefaultHttpClient();HttpUriRequest req = new HttpGet(url);HttpResponse resp = client.execute(req);// String strResult =//EntityUtils.toString(resp.getEntity());// Log.i("weather->", strResult);// 一华氏度等于9/5摄氏度数值+32HttpEntity ent = resp.getEntity();InputStream stream = ent.getContent();DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();Document d = b.parse(newInputSource(stream));NodeList n =d.getElementsByTagName("forecast_conditions");// 获得图片url 当天的。
String imgUrl = "";imgUrl +=n.item(0).getChildNodes().item(3).getAttributes().item(0).getNodeValue();imView.setImageBitmap(Utils.returnBitMap(imgUrl));// 今后4天预报for (int i = 0; i < n.getLength(); i++) {weather +=Utils.week(n.item(i).getChildNodes().item(0).getAttributes().item(0).getNodeValue());weather += ", ";weather += (Integer.parseInt(n.item(i).getChildNodes().item(1).getAttributes().item(0).getNodeValue()) - 32) * 5 / 9;weather += " ~ ";weather += (Integer.parseInt(n.item(i).getChildNodes().item(2).getAttributes().item(0).getNodeValue()) - 32) * 5 / 9;weather += ", ";weather += Utils.weather(n.item(i).getChildNodes().item(4).getAttributes().item(0).getNodeValue());weather += "\n";}Log.i("parseed weather->", weather);TvPlace.setText(weather);} catch (Exception e) {e.printStackTrace();}}});}}Utils类:package com.AndroidWeather;import java.io.IOException;import java.io.InputStream;import .HttpURLConnection;import .MalformedURLException;import .URL;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class Utils {public static String week(String enWeek) {if (enWeek.equals("Mon") || enWeek.equals("Monday"))return"星期一";else if (enWeek.equals("Tue") || enWeek.equals("Tuesday"))return"星期二";else if (enWeek.equals("Wed") ||enWeek.equals("Wednesday"))return"星期三";else if (enWeek.equals("Thu") ||enWeek.equals("Thursday"))return"星期四";else if (enWeek.equals("Fri") || enWeek.equals("Friday"))return"星期五";else if (enWeek.equals("Sat") ||enWeek.equals("Saturday"))return"星期六";else if (enWeek.equals("Sun") || enWeek.equals("Sunday"))return"星期日";return"";}public static String weather(String enWeather) {if (enWeather.equals("Clear"))return"晴";else if (enWeather.equals("Partly Sunny")|| enWeather.equals("partly_cloudy"))return"多云";else if (enWeather.equals("Chance of Rain"))return"晴转雨";else if (enWeather.equals("storm"))return"暴雨";else if (enWeather.equals("thunderstorm"))return"雷阵雨";else if (enWeather.equals("fog"))return"大雾";else if (enWeather.equals("haze"))return"有雾";else if (enWeather.equals("rain"))return"雨";else if (enWeather.equals("heavyrain"))return"大雨";else if (enWeather.equals("lightrain"))return"小雨";else if (enWeather.equals("heavyrain"))return"大雨";else if (enWeather.equals("snow"))return"有雪";// / 还需要补充。