当前位置:文档之家› Android 中文天气预报程序

Android 中文天气预报程序

/einyboy/blog/item/1cb8c9656cb43ffef6365404.htmlAndroid 中文天气预报程序2010年09月13日星期一12:47大家好!由于项目原因好长时间没学android了。

今天抽出点儿时间做个天气预报程序。

贴出来希望对大家有用。

先说一下Google天气预报的AIP吧。

/ig/api?&weather=城市名(中文全拼).我发现在模拟器上面返回的结果和在pc上返回的结果不太一样,pc上有中文的,android 模拟器上没有的。

好了不说别的了。

开始吧!now start。

1。

建立工程。

我取名sbsWeather。

结构如下:我们一步一步来,慢慢完善2。

我习惯先写布局。

1.2.<?xml version="1.0" encoding="utf-8"?>3.<LinearLayout xmlns:android="/apk/res/android"4.android:orientation="vertical"5.android:layout_width="fill_parent"6.android:layout_height="fill_parent"7.>8.<TextView9.android:id="@+id/tvPlace"10.android:layout_width="fill_parent"11.android:layout_height="wrap_content"12./>13.<EditText14.android:id="@+id/place"15.android:layout_width="fill_parent"16.android:layout_height="wrap_content"17.android:hint="输入城市名称(汉字或者拼音)"18./>19.<Button20.android:id="@+id/query"21.android:layout_width="fill_parent"22.android:layout_height="wrap_content"23.android:text="查询"24./>25.</LinearLayout>复制代码3。

我们要做的是中文天气预报哦。

可是Google为我们返回的是中文。

再有我们得把用户输入的中文转换为拼音再传给Google才可以。

现在,需要准备一些工具。

编写中文拼音转换类,CntoEn.java。

网上好多的。

自己随便找一个就哦了。

这个不贴代码了。

搜Java 中文转音频即可。

Utils.java 转换Google返回的英文。

一对一的转换吧,好像没别的跟好的办法。

另外其中还有个方法是将网络图片转换为android的bitmap对象的。

1.2.package com.sbs.weather;3.import java.io.IOException;4.import java.io.InputStream;5.import .HttpURLConnection;6.import .MalformedURLException;7.import .URL;8.import android.graphics.Bitmap;9.import android.graphics.BitmapFactory;10.public class Utils {11.public static String week(String enWeek) {12.if (enWeek.equals("Mon") || enWeek.equals("Monday"))13.return "星期一";14.else if (enWeek.equals("Tue") || enWeek.equals("Tuesday"))15.return "星期二";16.else if (enWeek.equals("Wed") || enWeek.equals("Wednesday"))17.return "星期三";18.else if (enWeek.equals("Thu") || enWeek.equals("Thursday"))19.return "星期四";20.else if (enWeek.equals("Fri") || enWeek.equals("Friday"))21.return "星期五";22.else if (enWeek.equals("Sat") || enWeek.equals("Saturday"))23.return "星期六";24.else if (enWeek.equals("Sun") || enWeek.equals("Sunday"))25.return "星期日";26.return "";27.}28.public static String weather(String enWeather) {29.if (enWeather.equals("Clear"))30.return "晴";31.else if (enWeather.equals("Partly Sunny")32.|| enWeather.equals("partly_cloudy"))33.return "多云";34.else if (enWeather.equals("Chance of Rain"))35.return "晴转雨";36.else if (enWeather.equals("storm"))37.return "暴雨";38.else if (enWeather.equals("thunderstorm"))39.return "雷阵雨";40.else if (enWeather.equals("fog"))41.return "大雾";42.else if (enWeather.equals("haze"))43.return "有雾";44.else if (enWeather.equals("rain"))45.return "雨";46.else if (enWeather.equals("heavyrain"))47.return "大雨";48.else if (enWeather.equals("lightrain"))49.return "小雨";50.else if (enWeather.equals("heavyrain"))51.return "大雨";52.else if (enWeather.equals("snow"))53.return "有雪";54.// / 还需要补充。

55.return "";56.}57.public static Bitmap returnBitMap(String imgUrl) {58.URL myImgUrl = null;59.Bitmap bitmap = null;60.try {61.myImgUrl = new URL(imgUrl);62.} catch (MalformedURLException e) {63.e.printStackTrace();64.}65.try {66.HttpURLConnection conn = (HttpURLConnection) myImgUrl67..openConnection();68.conn.setDoInput(true);69.conn.connect();70.InputStream is = conn.getInputStream();71.bitmap = BitmapFactory.decodeStream(is);72.is.close();73.} catch (IOException e) {74.e.printStackTrace();75.}76.return bitmap;77.}78.}复制代码4。

开始真正的工作咯。

下面的程序通过Google提供的api来获取天气预报信息。

1.package com.sbs.weather;2.3.import java.io.InputStream;4.5.import javax.xml.parsers.DocumentBuilder;6.import javax.xml.parsers.DocumentBuilderFactory;7.8.import org.apache.http.HttpEntity;9.import org.apache.http.HttpResponse;10.import org.apache.http.client.methods.HttpGet;11.import org.apache.http.client.methods.HttpUriRequest;12.import org.apache.http.impl.client.DefaultHttpClient;13.import org.apache.http.util.EntityUtils;14.import org.w3c.dom.Document;15.import org.w3c.dom.NodeList;16.import org.xml.sax.InputSource;17.18.import android.app.Activity;19.import android.graphics.Bitmap;20.import android.os.Bundle;21.import android.os.Handler;22.import android.os.Message;23.import android.util.Log;24.import android.view.View;25.import android.widget.Button;26.import android.widget.EditText;27.import android.widget.ImageView;28.import android.widget.TextView;29.30.public class Weather extends Activity {31.32.public EditText ETplace;33.public TextView TvPlace;34.public Button query;35.public TextView placeName;36.public ImageView imView;37.38./** Called when the activity is first created. */39.@Override40.public void onCreate(Bundle savedInstanceState) {41.super.onCreate(savedInstanceState);42.setContentView(yout.main);43.44.ETplace = (EditText)findViewById(R.id.place);45.query = (Button)findViewById(R.id.query);46.imView = (ImageView)findViewById(R.id.myImageView);47.placeName = (TextView)findViewById(R.id.placeName);48.49.query.setOnClickListener(new Button.OnClickListener() {50.public void onClick(View v) {51.try{Place = (TextView)findViewById(Place);53.54.String place = CntoEn.getFullSpell(ETplace.getText().toString());55.placeName.setText(place);56.String weather = "";57.String url = "/ig/api?&weather="+place;58.DefaultHttpClient client = new DefaultHttpClient();59.HttpUriRequest req = new HttpGet(url);60.HttpResponse resp = client.execute(req);61.62.//String strResult = EntityUtils.toString(resp.getEntity());63.//Log.i("weather->", strResult);64.//一华氏度等于9/5摄氏度数值+3265.66.HttpEntity ent = resp.getEntity();67.InputStream stream = ent.getContent();68.69.DocumentBuilder b = DocumentBuilderFactory.newInstance()70..newDocumentBuilder();71.Document d = b.parse(new InputSource(stream));72.NodeList n = d.getElementsByTagName("forecast_conditions");73.74.// 获得图片url 当天的。

相关主题