版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、通过google接口在Android中实现天气预报效果Android可以通过google实现获取指定经纬度位置或者某一个城市的天气信息。如果是根据经纬度查询天气信息,需要对精度为进行转换,例如lat值为31.174165,需要过滤掉小数点,变为31174165传到接口中,维度也一样处理,处理后传给 latI = (int) (lat * 1E6);int lonI = (int) (lon * 1E6);下面的例子演示了根据输入城市,获取该城市的天气预报,Weather.java的61行,是根据经纬度获取天气信息。工程结构:Weather.java类package com.Andr
2、oidWeather;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;impo
3、rt 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
4、.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 Tex
5、tView placeName;public ImageView imView;/* Called when the activity is first created. */Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main1);ETplace = (EditText) findViewById(R.id.place);query = (Button) findViewById(R.id.query);im
6、View = (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 = ETplace.getText().toString(); String place = CntoS
7、pell.getFullSpell(ETplace.getText().toString();placeName.setText(place);String weather = ""/String url = "/+ place;String url = "DefaultHttpClient client = new DefaultHttpClient();HttpUriRequest req = new HttpGet(url);HttpResponse resp = client.execute(req);/ String strResult =/
8、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(new InputSource(stream);Nod
9、eList 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 +=
10、 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).getChildNod
11、es().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 (Excep
12、tion 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(
13、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 "星期三&
14、quot;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 "星期六"
15、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("pa
16、rtly_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
17、 "大雾"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.e
18、quals("heavyrain")return "大雨"else if (enWeather.equals("snow")return "有雪"/ / 还需要补充。return ""public static Bitmap returnBitMap(String imgUrl) URL myImgUrl = null;Bitmap bitmap = null;try myImgUrl = new URL(imgUrl); catch (MalformedURLException e) e.pr
19、intStackTrace();try HttpURLConnection conn = (HttpURLConnection) myImgUrl.openConnection();conn.setDoInput(true);conn.connect();InputStream is = conn.getInputStream();bitmap = BitmapFactory.decodeStream(is);is.close(); catch (IOException e) e.printStackTrace();return bitmap;CntoSpell类:package com.An
20、droidWeather;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Set;public class CntoSpell private static LinkedHashMap spellMap = null; static if (spellMap = null) spellMap = new LinkedHashMap(400); initialize(); / System.out.println("Chinese transfer Spell Done.");
21、 private CntoSpell() private static void spellPut(String spell, int ascii) spellMap.put(spell, new Integer(ascii); private static void initialize() spellPut("a", -20319); spellPut("ai", -20317); spellPut("an", -20304); spellPut("ang", -20295); spellPut("a
22、o", -20292); spellPut("ba", -20283); spellPut("bai", -20265); spellPut("ban", -20257); spellPut("bang", -20242); spellPut("bao", -20230); spellPut("bei", -20051); spellPut("ben", -20036); spellPut("beng", -20032); s
23、pellPut("bi", -20026); spellPut("bian", -20002); spellPut("biao", -19990); spellPut("bie", -19986); spellPut("bin", -19982); spellPut("bing", -19976); spellPut("bo", -19805); spellPut("bu", -19784); spellPut("ca&quo
24、t;, -19775); spellPut("cai", -19774); spellPut("can", -19763); spellPut("cang", -19756); spellPut("cao", -19751); spellPut("ce", -19746); spellPut("ceng", -19741); spellPut("cha", -19739); spellPut("chai", -19728); spell
25、Put("chan", -19725); spellPut("chang", -19715); spellPut("chao", -19540); spellPut("che", -19531); spellPut("chen", -19525); spellPut("cheng", -19515); spellPut("chi", -19500); spellPut("chong", -19484); spellPut("c
26、hou", -19479); spellPut("chu", -19467); spellPut("chuai", -19289); spellPut("chuan", -19288); spellPut("chuang", -19281); spellPut("chui", -19275); spellPut("chun", -19270); spellPut("chuo", -19263); spellPut("ci",
27、-19261); spellPut("cong", -19249); spellPut("cou", -19243); spellPut("cu", -19242); spellPut("cuan", -19238); spellPut("cui", -19235); spellPut("cun", -19227); spellPut("cuo", -19224); spellPut("da", -19218); spellPut(&q
28、uot;dai", -19212); spellPut("dan", -19038); spellPut("dang", -19023); spellPut("dao", -19018); spellPut("de", -19006); spellPut("deng", -19003); spellPut("di", -18996); spellPut("dian", -18977); spellPut("diao", -18
29、961); spellPut("die", -18952); spellPut("ding", -18783); spellPut("diu", -18774); spellPut("dong", -18773); spellPut("dou", -18763); spellPut("du", -18756); spellPut("duan", -18741); spellPut("dui", -18735); spellPut(&qu
30、ot;dun", -18731); spellPut("duo", -18722); spellPut("e", -18710); spellPut("en", -18697); spellPut("er", -18696); spellPut("fa", -18526); spellPut("fan", -18518); spellPut("fang", -18501); spellPut("fei", -18490); s
31、pellPut("fen", -18478); spellPut("feng", -18463); spellPut("fo", -18448); spellPut("fou", -18447); spellPut("fu", -18446); spellPut("ga", -18239); spellPut("gai", -18237); spellPut("gan", -18231); spellPut("gang&quo
32、t;, -18220); spellPut("gao", -18211); spellPut("ge", -18201); spellPut("gei", -18184); spellPut("gen", -18183); spellPut("geng", -18181); spellPut("gong", -18012); spellPut("gou", -17997); spellPut("gu", -17988); spellPu
33、t("gua", -17970); spellPut("guai", -17964); spellPut("guan", -17961); spellPut("guang", -17950); spellPut("gui", -17947); spellPut("gun", -17931); spellPut("guo", -17928); spellPut("ha", -17922); spellPut("hai"
34、, -17759); spellPut("han", -17752); spellPut("hang", -17733); spellPut("hao", -17730); spellPut("he", -17721); spellPut("hei", -17703); spellPut("hen", -17701); spellPut("heng", -17697); spellPut("hong", -17692); spellPu
35、t("hou", -17683); spellPut("hu", -17676); spellPut("hua", -17496); spellPut("huai", -17487); spellPut("huan", -17482); spellPut("huang", -17468); spellPut("hui", -17454); spellPut("hun", -17433); spellPut("huo"
36、, -17427); spellPut("ji", -17417); spellPut("jia", -17202); spellPut("jian", -17185); spellPut("jiang", -16983); spellPut("jiao", -16970); spellPut("jie", -16942); spellPut("jin", -16915); spellPut("jing", -16733); spell
37、Put("jiong", -16708); spellPut("jiu", -16706); spellPut("ju", -16689); spellPut("juan", -16664); spellPut("jue",-16657); spellPut("jun", -16647); spellPut("ka", -16474); spellPut("kai", -16470); spellPut("kan",
38、 -16465); spellPut("kang", -16459); spellPut("kao", -16452); spellPut("ke", -16448); spellPut("ken", -16433); spellPut("keng", -16429); spellPut("kong", -16427); spellPut("kou", -16423); spellPut("ku", -16419); spellPut(
39、"kua", -16412); spellPut("kuai", -16407); spellPut("kuan", -16403); spellPut("kuang", -16401); spellPut("kui", -16393); spellPut("kun", -16220); spellPut("kuo", -16216); spellPut("la", -16212); spellPut("lai",
40、-16205); spellPut("lan", -16202); spellPut("lang", -16187); spellPut("lao", -16180); spellPut("le", -16171); spellPut("lei", -16169); spellPut("leng", -16158); spellPut("li", -16155); spellPut("lia", -15959); spellPut(&q
41、uot;lian", -15958); spellPut("liang", -15944); spellPut("liao", -15933); spellPut("lie", -15920); spellPut("lin", -15915); spellPut("ling", -15903); spellPut("liu", -15889); spellPut("long", -15878); spellPut("lou",
42、 -15707); spellPut("lu", -15701); spellPut("lv", -15681); spellPut("luan", -15667); spellPut("lue", -15661); spellPut("lun", -15659); spellPut("luo", -15652); spellPut("ma", -15640); spellPut("mai", -15631); spellPut(&qu
43、ot;man", -15625); spellPut("mang", -15454); spellPut("mao", -15448); spellPut("me", -15436); spellPut("mei", -15435); spellPut("men", -15419); spellPut("meng", -15416); spellPut("mi", -15408); spellPut("mian", -1539
44、4); spellPut("miao", -15385); spellPut("mie", -15377); spellPut("min", -15375); spellPut("ming", -15369); spellPut("miu", -15363); spellPut("mo", -15362); spellPut("mou", -15183); spellPut("mu", -15180); spellPut("n
45、a", -15165); spellPut("nai", -15158); spellPut("nan", -15153); spellPut("nang", -15150); spellPut("nao", -15149); spellPut("ne", -15144); spellPut("nei", -15143); spellPut("nen", -15141); spellPut("neng", -15140); s
46、pellPut("ni", -15139); spellPut("nian", -15128); spellPut("niang", -15121); spellPut("niao", -15119); spellPut("nie", -15117); spellPut("nin", -15110); spellPut("ning", -15109); spellPut("niu", -14941); spellPut("no
47、ng", -14937); spellPut("nu", -14933); spellPut("nv", -14930); spellPut("nuan", -14929); spellPut("nue", -14928); spellPut("nuo", -14926); spellPut("o", -14922); spellPut("ou", -14921); spellPut("pa", -14914); spellP
48、ut("pai", -14908); spellPut("pan", -14902); spellPut("pang", -14894); spellPut("pao", -14889); spellPut("pei", -14882); spellPut("pen", -14873); spellPut("peng", -14871); spellPut("pi", -14857); spellPut("pian"
49、, -14678); spellPut("piao", -14674); spellPut("pie", -14670); spellPut("pin", -14668); spellPut("ping", -14663); spellPut("po", -14654); spellPut("pu", -14645); spellPut("qi", -14630); spellPut("qia", -14594); spellPut(&
50、quot;qian", -14429); spellPut("qiang", -14407); spellPut("qiao", -14399); spellPut("qie", -14384); spellPut("qin", -14379); spellPut("qing", -14368); spellPut("qiong", -14355); spellPut("qiu", -14353); spellPut("qu"
51、, -14345); spellPut("quan", -14170); spellPut("que", -14159); spellPut("qun", -14151); spellPut("ran", -14149); spellPut("rang", -14145); spellPut("rao", -14140); spellPut("re", -14137); spellPut("ren", -14135); spellPut
52、("reng", -14125); spellPut("ri", -14123); spellPut("rong", -14122); spellPut("rou", -14112); spellPut("ru", -14109); spellPut("ruan", -14099); spellPut("rui", -14097); spellPut("run", -14094); spellPut("ruo", -
53、14092); spellPut("sa", -14090); spellPut("sai", -14087); spellPut("san", -14083); spellPut("sang", -13917); spellPut("sao", -13914); spellPut("se", -13910); spellPut("sen", -13907); spellPut("seng", -13906); spellPut(&qu
54、ot;sha", -13905); spellPut("shai", -13896); spellPut("shan", -13894); spellPut("shang", -13878); spellPut("shao", -13870); spellPut("she", -13859); spellPut("shen", -13847); spellPut("sheng", -13831); spellPut("shi"
55、, -13658); spellPut("shou", -13611); spellPut("shu",-13601);spellPut("shua", -13406); spellPut("shuai", -13404); spellPut("shuan", -13400); spellPut("shuang", -13398); spellPut("shui", -13395); spellPut("shun", -13391);
56、spellPut("shuo", -13387); spellPut("si", -13383); spellPut("song", -13367); spellPut("sou", -13359); spellPut("su", -13356); spellPut("suan", -13343); spellPut("sui", -13340); spellPut("sun", -13329); spellPut("suo&
57、quot;, -13326); spellPut("ta", -13318); spellPut("tai", -13147); spellPut("tan", -13138); spellPut("tang", -13120); spellPut("tao", -13107); spellPut("te", -13096); spellPut("teng", -13095); spellPut("ti", -13091); spell
58、Put("tian", -13076); spellPut("tiao", -13068); spellPut("tie", -13063); spellPut("ting", -13060); spellPut("tong", -12888); spellPut("tou", -12875); spellPut("tu", -12871); spellPut("tuan", -12860); spellPut("tui", -12858); spellPut("tun", -12852); spellPut("tuo", -12849); spellPut("wa", -12838); spellPut("wai",
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年湖南湘江新区发展集团有限公司公开招聘备考题库及一套参考答案详解
- 2025年杭州市第三人民医院公开招聘编外工作人员5人备考题库及参考答案详解1套
- 2025年烟台交通集团有限公司管理培训生招聘备考题库有答案详解
- 2025年河北邯郸武安市公开招聘食品检测专业技术人员4名备考考试试题及答案解析
- 个旧市教体系统2026年事业单位校园公开招聘备考题库及参考答案详解一套
- 2025年吴川市县域医疗卫生共同体公开招聘编制外工作人员134人备考题库及完整答案详解一套
- 2025年富宁县紧密型医共体田蓬一中心分院公开招聘编外合同制人员的备考题库及1套完整答案详解
- 广州中医药大学梅州医院(梅州市中医医院、梅州市田家炳医院)2026年第一批公开招聘聘用人员备考题库及答案详解一套
- 2026年浙江省温岭市卫生事业单位公开招聘医学卫生类高学历人才备考题库及一套完整答案详解
- 2025年河南中原国际会展中心有限公司社会招聘44名备考考试题库及答案解析
- GB/T 27995.1-2025半成品镜片毛坯第1部分:单焦和多焦
- 护理部主任年终汇报
- 《电力市场概论》 课件 第七章 发电投资分析
- 2024年新苏教版四年级上册科学全册知识点(复习资料)
- 题库二附有答案
- 市场拓展与销售渠道拓展方案
- 工地大门施工协议书
- 铁血将军、建军元勋-叶挺 (1)讲解
- 2023年西门子PLC知识考试题(附含答案)
- 鼻鼽(变应性鼻炎)诊疗方案
- 消防应急疏散和灭火演习技能培训
评论
0/150
提交评论