#android天气预报源代码.docx
《#android天气预报源代码.docx》由会员分享,可在线阅读,更多相关《#android天气预报源代码.docx(28页珍藏版)》请在冰豆网上搜索。
#android天气预报源代码
通过google接口在Android中实现天气预报效果
Android可以通过google实现获取指定经纬度位置或者某一个城市的天气信息。
如果是根据经纬度查询天气信息,需要对精度为进行转换,例如lat值为31.174165,需要过滤掉小数点,变为31174165传到接口中,维度也一样处理,处理后传给 既可以获取数据。
这里要注意一个问题,如果大家获取的经纬度序列很长,直接去掉小数点,有时候也无法获取天气信息,例如40.478224838152528,124.97828006744385,去掉小数点后,传到参数位置,无法获取值,需要大家将经纬度按下面方式转换一下,只取小数点后6位就可以了。
intlatI=(int>(lat*1E6>。
intlonI=(int>(lon*1E6>。
下面的例子演示了根据输入城市,获取该城市的天气预报,Weather.java的61行,是根据经纬度获取天气信息。
项目结构:
Weather.java类
packagecom.AndroidWeather。
importjava.io.InputStream。
importjavax.xml.parsers.DocumentBuilder。
importjavax.xml.parsers.DocumentBuilderFactory。
importorg.apache.http.HttpEntity。
importorg.apache.http.HttpResponse。
importorg.apache.http.client.methods.HttpGet。
importorg.apache.http.client.methods.HttpUriRequest。
importorg.apache.http.impl.client.DefaultHttpClient。
importorg.apache.http.util.EntityUtils。
importorg.w3c.dom.Document。
importorg.w3c.dom.NodeList。
importorg.xml.sax.InputSource。
importandroid.app.Activity。
importandroid.graphics.Bitmap。
importandroid.os.Bundle。
importandroid.os.Handler。
importandroid.os.Message。
importandroid.util.Log。
importandroid.view.View。
importandroid.widget.Button。
importandroid.widget.EditText。
importandroid.widget.ImageView。
importandroid.widget.TextView。
publicclassWeatherextendsActivity{
publicEditTextETplace。
publicTextViewTvPlace。
publicButtonquery。
publicTextViewplaceName。
publicImageViewimView。
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState>{
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(newButton.OnClickListener(>{
publicvoidonClick(Viewv>{
try{
TvPlace=(TextView>findViewById(R.id.tvPlace>。
//Stringplace=ETplace.getText(>.toString(>。
Stringplace=
CntoSpell.getFullSpell(ETplace.getText(>.toString(>>。
placeName.setText(place>。
Stringweather=""。
//Stringurl=""
//+place。
Stringurl=""。
DefaultHttpClientclient=newDefaultHttpClient(>。
HttpUriRequestreq=newHttpGet(url>。
HttpResponseresp=client.execute(req>。
//StringstrResult=
//EntityUtils.toString(resp.getEntity(>>。
//Log.i("weather->",strResult>。
//一华氏度等于9/5摄氏度数值+32
HttpEntityent=resp.getEntity(>。
InputStreamstream=ent.getContent(>。
DocumentBuilderb=DocumentBuilderFactory.newInstance(>
.newDocumentBuilder(>。
Documentd=b.parse(newInputSource(stream>>。
NodeListn=d.getElementsByTagName("forecast_conditions">。
//获得图片url当天的。
StringimgUrl=""。
imgUrl+=n.item(0>.getChildNodes(>.item(3>.getAttributes(>
.item(0>.getNodeValue(>。
imView.setImageBitmap(Utils.returnBitMap(imgUrl>>。
//今后4天预报
for(inti=0。
i。
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("parseedweather->",weather>。
TvPlace.setText(weather>。
}catch(Exceptione>{
e.printStackTrace(>。
}
}
}>。
}
}
Utils类:
packagecom.AndroidWeather。
importjava.io.IOException。
importjava.io.InputStream。
import.HttpURLConnection。
import.MalformedURLException。
import.URL。
importandroid.graphics.Bitmap。
importandroid.graphics.BitmapFactory。
publicclassUtils{
publicstaticStringweek(StringenWeek>{
if(enWeek.equals("Mon">||enWeek.equals("Monday">>
return"星期一"。
elseif(enWeek.equals("Tue">||enWeek.equals("Tuesday">>
return"星期二"。
elseif(enWeek.equals("Wed">||enWeek.equals("Wednesday">>
return"星期三"。
elseif(enWeek.equals("Thu">||enWeek.equals("Thursday">>
return"星期四"。
elseif(enWeek.equals("Fri">||enWeek.equals("Friday">>
return"星期五"。
elseif(enWeek.equals("Sat">||enWeek.equals("Saturday">>
return"星期六"。
elseif(enWeek.equals("Sun">||enWeek.equals("Sunday">>
return"星期日"。
return""。
}
publicstaticStringweather(StringenWeather>{
if(enWeather.equals("Clear">>
return"晴"。
elseif(enWeather.equals("PartlySunny">
||enWeather.equals("partly_cloudy">>
return"多云"。
elseif(enWeather.equals("ChanceofRain">>
return"晴转雨"。
elseif(enWeather.equals("storm">>
return"暴雨"。
elseif(enWeather.equals("thunderstorm">>
return"雷阵雨"。
elseif(enWeather.equals("fog">>
return"大雾"。
elseif(enWeather.equals("haze">>
return"有雾"。
elseif(enWeather.equals("rain">>
return"雨"。
elseif(enWeather.equals("heavyrain">>
return"大雨"。
elseif(enWeather.equals("lightrain">>
return"小雨"。
elseif(enWeather.equals("heavyrain">>
return"大雨"。
elseif(enWeather.equals("snow">>
return"有雪"。
///还需要补充。
。
。
。
return""。
}
publicstaticBitmapreturnBitMap(StringimgUrl>{
URLmyImgUrl=null。
Bitmapbitmap=null。
try{
myImgUrl=newURL(imgUrl>。
}catch(MalformedURLExceptione>{
e.printStackTrace(>。
}
try{
HttpURLConnectionconn=(HttpURLConnection>myImgUrl
.openConnection(>。
conn.setDoInput(true>。
conn.connect(>。
InputStreamis=conn.getInputStream(>。
bitmap=BitmapFactory.decodeStream(is>。
is.close(>。
}catch(IOExceptione>{
e.printStackTrace(>。
}
returnbitmap。
}
}
CntoSpell类:
packagecom.AndroidWeather。
importjava.util.Iterator。
importjava.util.LinkedHashMap。
importjava.util.Set。
publicclassCntoSpell{
privatestaticLinkedHashMapspellMap=null。
static{
if(spellMap==null>{
spellMap=newLinkedHashMap(400>。
}
initialize(>。
//System.out.println("ChinesetransferSpellDone.">。
}
privateCntoSpell(>{
}
privatestaticvoidspellPut(Stringspell,intascii>{
spellMap.put(spell,newInteger(ascii>>。
}
privatestaticvoidinitialize(>{
spellPut("a",-20319>。
spellPut("ai",-20317>。
spellPut("an",-20304>。
spellPut("ang",-20295>。
spellPut("ao",-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>。
spellPut("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",-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>。
spellPut("chan",-19725>。
spellPut("chang",-19715>。
spellPut("chao",-19540>。
spellPut("che",-19531>。
spellPut("chen",-19525>。
spellPut("cheng",-19515>。
spellPut("chi",-19500>。
spellPut("chong",-19484>。
spellPut("chou",-19479>。
spellPut("chu",-19467>。
spellPut("chuai",-19289>。
spellPut("chuan",-19288>。
spellPut("chuang",-19281>。
spellPut("chui",-19275>。
spellPut("chun",-19270>。
spellPut("chuo",-19263>。
spellPut("ci",-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("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",-18961>。
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("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>。
spellPut("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",-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>。
spellPut("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",-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>。
spellPut("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",-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>。
spellPut("jiong",-16708>。
spellPut("jiu",-16706>。