android天气预报源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

通过google接口在Android中实现天气预报效果Android可以通过google实现获取指定经纬度位置或者某一个城市的天气信息。如果是根据经纬度查询天气信息,需要对精度为进行转换,例如lat值为31.174165,需要过滤掉小数点,变为31174165传到接口中,维度也一样处理,处理后传给=,,,31174165,121433841既可以获取数据。这里要注意一个问题,如果大家获取的经纬度序列很长,直接去掉小数点,有时候也无法获取天气信息,例如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.*/@OverridepublicvoidonCreate(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==,,,31174165,121433841;DefaultHttpClientclient=newDefaultHttpClient();HttpUriRequestreq=newHttpGet(url);HttpResponseresp=client.execute(req);//StringstrResult=//EntityUtils.toString(resp.getEntity());//Log.i(weather-,strResult);//一华氏度等于9/5摄氏度数值+32HttpEntityent=resp.getEntity();InputStreamstream=ent.getContent();DocumentBuilderb=DocumentBuilderFactory.newInstance().newDocumentBuilder();Documentd=b.parse(newInputSource(stream));NodeListn=d.getElementsByTagName(forecast_conditions);//获得图片url当天的。StringimgUrl==n.item(0).getChildNodes().item(3).getAttributes().item(0).getNodeValue();imView.setImageBitmap(Utils.returnBitMap(imgUrl));//今后4天预报for(inti=0;in.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(parseedweather-,weather);TvPlace.setText(weather);}catch(Exceptione){e.printStackTrace();}}});}}Utils类:packagecom.AndroidWeather;importjava.io.IOException;importjava.io.InputStream;importjava.net.HttpURLConnection;importjava.net.MalformedURLException;importjava.net.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

1 / 24
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功