【Android源码 栏目提醒】:网学会员为需要Android源码 的朋友们搜集整理了android天气预报源代码 - 技术总结相关资料,希望对各位网友有所帮助!
通过google接口在
Android中实现天气预报效果
Android可以通过google实现获取指定经纬度位置或者某一个城市的天气信息。
如果是根据经纬度查询天气信息需要对精度为迚行转换例如lat值为31.174165需要过滤掉小数点变为31174165传到接口中维度也一样处理处理后传给 http://www.google.com/ig/apiweather31174 既可以获取数据。
这里要注意一个问题如果大家获取的经纬度序列很长直接去掉小数点有时候也无法获取天气信息例如40.478224838152528124.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. / Override public void onCreateBundle savedInstanceState super.onCreatesavedInstanceState setContentViewR.layout.main1 ETplace EditText findViewByIdR.id.place query Button findViewByIdR.id.query imView ImageView findViewByIdR.id.myImageView placeName TextView findViewByIdR.id.tvPlace query.setOnClickListenernew Button.OnClickListener public void onClickView v try TvPlace TextView findViewByIdR.id.tvPlace // String place ETplace.getText.toString String place CntoSpell.getFullSpellETplace.getText.toString placeName.setTextplace String weather quotquot // String url quothttp://www.google.com/ig/apiampweatherquot // place String url quothttp://www.google.com/ig/apiweather31174quot DefaultHttpClient client new DefaultHttpClient HttpUriRequest req new HttpGeturl HttpResponse resp client.executereq // String strResult // EntityUtils.toStringresp.getEntity // Log.iquotweather-gtquot strResult // 一华氏度等于9/5摄氏度数值32 HttpEntity ent resp.getEntity InputStream stream ent.getContent DocumentBuilder b DocumentBuilderFactory.newInstance .newDocumentBuilder Document d b.parsenew InputSourcestream NodeList n d.getElementsByTagNamequotforecast_conditionsquot // 获得图片url 当天的。
String imgUrl quothttp://www.google.comquot imgUrl n.item0.getChildNodes.item3.getAttributes .item0.getNodeValue imView.setImageBitmapUtils.returnBitMapimgUrl // 今后4天预报 for int i 0 i lt n.getLength i weather Utils.weekn.itemi.getChildNodes.item0 .getAttributes.item0.getNodeValue weather quot quot weather Integer .parseIntn.itemi.getChildNodes.item1 .getAttributes.item0.getNodeValue - 32 5 / 9 weather quot quot weather Integer .parseIntn.itemi.getChildNodes.item2 .getAttributes.item0.getNodeValue - 32 5 / 9 weather quot quot weather Utils .weathern.itemi.getChildNodes.item4 .getAttributes.item0.getNodeValue weather quotnquot Log.iquotparseed weather-gtquot weather TvPlace.setTextweather catch Exception e e.printStackTrace Utils类 package com.AndroidWeather import java.io.IOException import java.io.InputStream import java.net.HttpURLConnection import java.net.MalformedURLException import java.net.URL import
android.graphics.Bitmap import
android.graphics.BitmapFactory public class Utils public static String weekString enWeek if enWeek.equalsquotMonquot enWeek.equalsquotMondayquot return quot星期一quot else if enWeek.equalsquotTuequot enWeek.equalsquotTuesdayquot return quot星期二quot else if enWeek.equalsquotWedquot enWeek.equalsquotWednesdayquot return quot星期三quot else if enWeek.equalsquotThuquot enWeek.equalsquotThursdayquot return quot星期四quot else if enWeek.equalsquotFriquot enWeek.equalsquotFridayquot return quot星期五quot else if enWeek.equalsquotSatquot enWeek.equalsquotSaturdayquot return quot星期六quot else if enWeek.equalsquotSunquot enWeek.equalsquotSundayquot return quot星期日quot return quotquot public static String weatherString enWeather if enWeather.equalsquotClearquot return quot晴quot else if enWeather.equalsquotPartly Sunnyquot enWeather.equalsquotpartly_cloudyquot return quot多云quot else if enWeather.equalsquotChance of Rainquot return quot晴转雨quot else if enWeather.equalsquotstormquot return quot暴雨quot else if enWeather.equalsquotthunderstormquot return quot雷阵雨quot else if enWeather.equalsquotfogquot return quot大雾quot else if enWeather.equalsquothazequot return quot有雾quot else if enWeather.equalsquotrainquot return quot雨quot else if enWeather.equalsquotheavyrainquot return quot大雨quot else if enWeather.equalsquotlightrainquot return quot小雨quot else if enWeather.equalsquotheavyrainquot return quot大雨quot else if enWeather.equalsquotsnowquot return quot有雪quot // / 还需要补充。
。
。
。
return quotquot public static Bitmap returnBitMapString imgUrl URL myImgUrl null Bitmap bitmap null try myImgUrl new URLimgUrl catch MalformedURLException e e.printStackTrace try HttpURLConnection conn HttpURLConnection myImgUrl .openConnection conn.setDoInputtrue conn.connect InputStream is conn.getInputStream bitmap BitmapFactory.decodeStreamis is.close catch IOException e e.printStackTrace return bitmap CntoSpell类 package com.AndroidWeather 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 LinkedHashMap400 initialize // System.out.printlnquotChinese transfer Spell Done.quot private CntoSpell private static void spellPutString spell int ascii spellMap.putspell new Integerascii private static void initialize spellPutquotaquot -20319 spellPutquotaiquot -20317 spellPutquotanquot -20304 spellPutquotangquot -20295 spellPutquotaoquot -20292 spellPutquotbaquot -20283 spellPutquotbaiquot -20265 spellPutquotbanquot -20257 spellPutquotbangquot -20242 spellPutquotbaoquot -20230 spellPutquotbeiquot -20051 spellPutquotbenquot -20036 spellPutquotbengquot -20032 spellPutquotbiquot -20026 spellPutquotbianquot -20002 spellPutquotbiaoquot -19990 spellPutquotbiequot -19986 spellPutquotbinquot -19982 spellPutquotbingquot -19976 spellPutquotboquot -19805 spellPutquotbuquot -19784 spellPutquotcaquot -19775 spellPutquotcaiquot -19774 spellPutquotcanquot -19763 spellPutquotcangquot -19756 spellPutquotcaoquot -19751 spellPutquotcequot -19746 spellPutquotcengquot -19741 spellPutquotchaquot -19739 spellPutquotchaiquot -19728 spellPutquotchanquot -19725 spellPutquotchangquot -19715 spellPutquotchaoquot -19540 spellPutquotchequot -19531 spellPutquotchenquot -19525 spellPutquotchengquot -19515 spellPutquotchiquot -19500 spellPutquotchongquot -19484 spellPutquotchouquot -19479 spellPutquotchuquot -19467 spellPutquotchuaiquot -19289 spellPutquotchuanquot -19288 spellPutquotchuangquot -19281 spellPutquotchuiquot -19275 spellPutquotchunquot -19270 spellPutquotchuoquot -19263 spellPutquotciquot -19261 spellPutquotcongquot -19249 spellPutquotcouquot -19243 spellPutquotcuquot -19242 spellPutquotcuanquot -19238 spellPutquotcuiquot -19235 spellPutquotcunquot -19227 spellPutquotcuoquot -19224 spellPutquotdaquot -19218 spellPutquotdaiquot -19212 spellPutquotdanquot -19038 spellPutquotdangquot -19023 spellPutquotdaoquot -19018 spellPutquotdequot -19006 spellPutquotdengquot -19003 spellPutquotdiquot -18996 spellPutquotdianquot -18977 spellPutquotdiaoquot -18961 spellPutquotdiequot -18952 spellPutquotdingquot -18783 spellPutquotdiuquot -18774 spellPutquotdongquot -18773 spellPutquotdouquot -18763 spellPutquotduquot -18756 spellPutquotduanquot -18741 spellPutquotduiquot -18735 spellPutquotdunquot -18731 spellPutquotduoquot -18722 spellPutquotequot -18710 spellPutquotenquot -18697 spellPutquoterquot -18696 spellPutquotfaquot -18526 spellPutquotfanquot -18518 spellPutquotfangquot -18501 spellPutquotfeiquot -18490 spellPutquotfenquot -18478 spellPutquotfengquot -18463 spellPutquotfoquot -18448 spellPutquotfouquot -18447 spellPutquotfuquot -18446 spellPutquotgaquot -18239 spellPutquotgaiquot -18237 spellPutquotganquot -18231 spellPutquotgangquot -18220 spellPutquotgaoquot -18211 spellPutquotgequot -18201 spellPutquotgeiquot -18184 spellPutquotgenquot -18183 spellPutquotgengquot -18181 spellPutquotgongquot -18012 spellPutquotgouquot -17997 spellPutquotguquot -17988 spellPutquotguaquot -17970 spellPutquotguaiquot -17964 spellPutquotguanquot -17961 spellPutquotguangquot -17950 spellPutquotguiquot -17947 spellPutquotgunquot -17931 spellPutquotguoquot -17928 spellPutquothaquot -17922 spellPutquothaiquot -17759 spellPutquothanquot -17752 spellPutquothangquot -17733 spellPutquothaoquot -17730 spellPutquothequot -17721 spellPutquotheiquot -17703 spellPutquothenquot -17701 spellPutquothengquot -17697 spellPutquothongquot -17692 spellPutquothouquot -17683 spellPutquothuquot -17676 spellPutquothuaquot -17496 spellPutquothuaiquot -17487 spellPutquothuanquot -17482 spellPutquothuangquot -17468 spellPutquothuiquot -17454 spellPutquothunquot -17433 spellPutquothuoquot -17427 spellPutquotjiquot -17417 spellPutquotjiaquot -17202 spellPutquotjianquot -17185 spellPutquotjiangquot -16983 spellPutquotjiaoquot -16970 spellPutquotjiequot -16942 spellPutquotjinquot -16915 spellPutquotjingquot -16733 spellPutquotjiongquot -16708 spellPutquotjiuquot -16706 spellPutquotjuquot -16689 spellPutquotjuanquot -16664 spellPutquotjuequot-16657 spellPutquotjunquot -16647 spellPutquotkaquot -16474 spellPutquotkaiquot -16470 spellPutquotkanquot -16465 spellPutquotkangquot -16459 spellPutquotkaoquot -16452 spellPutquotkequot -16448 spellPutquotkenquot -16433 spellPutquotkengquot -16429 spellPutquotkongquot -16427 spellPutquotkouquot -16423 spellPutquotkuquot -16419 spellPutquotkuaquot -16412 spellPutquotkuaiquot -16407 spellPutquotkuanquot -16403 spellPutquotkuangquot -16401 spellPutquotkuiquot -16393 spellPutquotkunquot -16220 spellPutquotkuoquot -16216 spellPutquotlaquot -16212 spellPutquotlaiquot -16205 spellPutquotlanquot -16202 spellPutquotlangquot -16187 spellPutquotlaoquot -16180 spellPutquotlequot -16171 spellPutquotleiquot -16169 spellPutquotlengquot -16158 spellPutquotliquot -16155 spellPutquotliaquot -15959 spellPutquotlianquot -15958 spellPutquotliangquot -15944 spellPutquotliaoquot -15933 spellPutquotliequot -15920 spellPutquotlinquot -15915 spellPutquotlingquot -15903 spellPutquotliuquot -15889 spellPutquotlongquot -15878 spellPutquotlouquot -15707 spellPutquotluquot -15701 spellPutquotlvquot -15681 spellPutquotluanquot -15667 spellPutquotluequot -15661 spellPutquotlunquot -15659 spellPutquotluoquot -15652 spellPutquotmaquot -15640 spellPutquotmaiquot -15631 spellPutquotmanquot -15625 spellPutquotmangquot -15454 spellPutquotmaoquot -15448 spellPutquotmequot -15436 spellPutquotmeiquot -15435 spellPutquotmenquot -15419 spellPutquotmengquot -15416 spellPutquotmiquot -15408 spellPutquotmianquot -15394 spellPutquotmiaoquot -15385 spellPutquotmiequot -15377 spellPutquotminquot -15375 spellPutquotmingquot -15369 spellPutquotmiuquot -15363 spellPutquotmoquot -15362 spellPutquotmouquot -15183 spellPutquotmuquot -15180 spellPutquotnaquot -15165 spellPutquotnaiquot -15158 spellPutquotnanquot -15153 spellPutquotnangquot -15150 spellPutquotnaoquot -15149 spellPutquotnequot -15144 spellPutquotneiquot -15143 spellPutquotnenquot -15141 spellPutquotnengquot -15140 spellPutquotniquot -15139 spellPutquotnianquot -15128 spellPutquotniangquot -15121 spellPutquotniaoquot -15119 spellPutquotniequot -15117 spellPutquotninquot -15110 spellPutquotningquot -15109 spellPutquotniuquot -14941 spellPutquotnongquot -14937 spellPutquotnuquot -14933 spellPutquotnvquot -14930 spellPutquotnuanquot -14929 spellPutquotnuequot -14928 spellPutquotnuoquot -14926 spellPutquotoquot -14922 spellPutquotouquot -14921 spellPutquotpaquot -14914 spellPutquotpaiquot -14908 spellPutquotpanquot -14902 spellPutquotpangquot -14894 spellPutquotpaoquot -14889 spellPutquotpeiquot -14882 spellPutquotpenquot -14873 spellPutquotpengquot -14871 spellPutquotpiquot -14857 spellPutquotpianquot -14678 spellPutquotpiaoquot -14674 spellPutquotpiequot -14670 spellPutquotpinquot -14668 spellPutquotpingquot -14663 spellPutquotpoquot -14654 spellPutquotpuquot -14645 spellPutquotqiquot -14630 spellPutquotqiaquot -14594 spellPutquotqianquot -14429 spellPutquotqiangquot -14407 spellPutquotqiaoquot -14399 spellPutquotqiequot -14384 spellPutquotqinquot -14379 spellPutquotqingquot -14368 spellPutquotqiongquot -14355 spellPutquotqiuquot -14353 spellPutquotququot -14345 spellPutquotquanquot -14170 spellPutquotquequot -14159 spellPutquotqunquot -14151 spellPutquotranquot -14149 spellPutquotrangquot -14145 spellPutquotraoquot -14140 spellPutquotrequot -14137 spellPutquotrenquot -14135 spellPutquotrengquot -14125 spellPutquotriquot -14123 spellPutquotrongquot -14122 spellPutquotrouquot -14112 spellPutquotruquot -14109 spellPutquotruanquot -14099 spellPutquotruiquot -14097 spellPutquotrunquot -14094 spellPutquotruoquot -14092 spellPutquotsaquot -14090 spellPutquotsaiquot -14087 spellPutquotsanquot -14083 spellPutquotsangquot -13917 spellPutquotsaoquot -13914 spellPutquotsequot -13910 spellPutquotsenquot -13907 spellPutquotsengquot -13906 spellPutquotshaquot -13905 sp.