通过google接口在Android中实现天气预报效果
Android可以通过google实现获取指定经纬度位置或者某一个城市的天气信息。如果是根据经纬度查询天气信息,需要对精度为进行转换,例如lat值为31.174165,需要过滤掉小数点,变为31174165传到接口中,维度也一样处理,处理后传给 le/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 l.parsers.DocumentBuilder;
import l.parsers.DocumentBuilderFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.hods.HttpGet;
import org.apache.hods.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 l.sax.InputSource;
import android.app.Activity;
import aphics.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. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
ETplace = (EditText) findViewById(R.id.place);
query = (Button) findViewById(R.id.query);
imView = (ImageView) findViewById(R.id.myImageView);
placeName = (TextView) findViewById(R.id.tvPlace);
query.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
TvPlace = (TextView) findViewById(R.id.tvPlace);
// String place = Text().toString();
String place =
CntoSpell.getFullSpell(ETplace.getText().toString());
placeName.setText(place);
String weather = "";
// String url = "le/ig/api?&weather="
// + place;
String url = "le/ig/api?weather=,,,31174165,121433841";
DefaultHttpClient client = new DefaultHttpClient();
HttpUriRequest req = new HttpGet(url);
HttpResponse resp = ute(req);
// String strResult =
// Entity());
// Log.i("weather->", strResult);
// 一华氏度等于9/5摄氏度数值+32
HttpEntity ent = Entity();
InputStream stream = Content();
DocumentBuilder b = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document d = b.parse(new InputSource(stream));
NodeList n = d.getElementsByTagName("forecast_conditions");
// 获得图片url 当天的。
String imgUrl = "le";
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 java.HttpURLConnection;
import java.MalformedURLException;
import java.URL;
import aphics.Bitmap;
import aphics.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"))
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论