获取网络天气数据---天气小软件
各位,我能写这篇文章得先感谢源码爱好者网站上网友免费提供的源代码,链接为http://www.codefans.net/soft/19603.shtml,对那些无私奉献的开源者们致以崇高的敬意,没有他们的无私奉献,就没有今天我们的进步。先请看效果图:这个界面排版起来有点复杂,我就直接贴出资源文件供大家参考:<LinearLayout xmlns:android="http:/
各位,我能写这篇文章得先感谢源码爱好者网站上网友免费提供的源代码,链接为http://www.codefans.net/soft/19603.shtml,对那些无私奉献的开源者们致以崇高的敬意,没有他们的无私奉献,就没有今天我们的进步。先请看效果图:
这个界面排版起来有点复杂,我就直接贴出资源文件供大家参考:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/rootLayout"
android:background="@drawable/weather" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="3px"
android:background="@drawable/item_top" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="@drawable/item_bg"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="30dp"
android:paddingRight="20dp" >
<TextView
android:id="@+id/date_y"
android:text="抬头看蓝天白云,或许会有新发现!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="28px"/>
<TextView
android:id="@+id/week"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="80px" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/item_bg">
<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:orientation="horizontal"
android:background="@drawable/top_bar">
<TextView
android:id="@+id/cityField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="25px"
android:gravity="bottom"
android:textColor="@color/white"
android:textSize="55dp" />
<TextView
android:id="@+id/currentWind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cityField"
android:layout_marginLeft="60dp"
android:layout_toRightOf="@+id/cityField"
android:gravity="bottom"
android:textColor="@color/white"
android:textSize="25dp" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/weather_icon01"
android:layout_width="200dp"
android:layout_height="200dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical" >
<TextView
android:id="@+id/currentWeather"
android:textSize="25dp"
android:textColor="@color/gold"
android:textColorHighlight="@color/lightgoldenrodyellow"
android:textColorHint="@color/darkgoldenrod"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/currentTemp"
android:textSize="20dp"
android:textColor="@color/gold"
android:textColorHighlight="@color/lightgoldenrodyellow"
android:textColorHint="@color/darkgoldenrod"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:singleLine="true"
android:scrollHorizontally="true"
android:focusableInTouchMode="true"
android:ellipsize="marquee"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" 呜呜...天气资讯数据被外星人劫走了"
android:id="@+id/index_d"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="25px"
android:layout_weight="0.03"
android:orientation="horizontal"
android:gravity="bottom"
android:background="@drawable/item_mid">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/item_bg" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom">
<TextView
android:text="明天"
android:textColor="@color/snow"
android:textSize="20dp"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:text="后天"
android:textColor="@color/snow"
android:textSize="20dp"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/weather_icon02"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/weather02"
android:gravity="top"
android:layout_gravity="center_vertical"
android:textColor="@color/snow"
android:textSize="13dp"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/temp02"
android:gravity="center_horizontal"
android:textColor="@color/snow"
android:textSize="12dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/wind02"
android:gravity="center_horizontal"
android:textColor="@color/snow"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/weather_icon03"
android:layout_width="80dp"
android:layout_height="80dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/weather03"
android:gravity="top"
android:layout_gravity="center_vertical"
android:textColor="@color/snow"
android:textSize="13dp" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/temp03"
android:gravity="center_horizontal"
android:textColor="@color/white"
android:textSize="12dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/wind03"
android:gravity="center_horizontal"
android:textColor="@color/snow"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10px"
android:background="@drawable/item_foot" />
</LinearLayout>
当时没加注解,大家可以对着效果图,慢慢熟悉一下。
接下来讲如何获取网络天气的,这里是获取中国天气网的数据。大体逻辑为刚打开时访问网络数据,如果成功,保存在本地;如果失败,访问本地保存的数据。
同样访问网络,得新定义一个线程,这里的cityCode是赣州的代码:101240701
Thread weather_thread=new Thread(new Runnable(){
@Override
public void run() {
try{
tools=new WebAccessTools();
Message msg=new Message();
String info = "http://m.weather.com.cn/data/"+cityCode+".html";
//=========================获取网络数据 ===============================
info=tools.getHtml(info);
if(info!=null){//网络数据不为空,更新天气
Log.i(TAG,info+"");
//==========================解析JSON得到天气信息===========================
json=new JSONObject(info).getJSONObject("weatherinfo");
Log.i(TAG,"get JsonData");
msg.what=JSONDATA_REDEY;
handler.sendMessage(msg);
dialog.dismiss();
}else {//网络数据为空,但本地数据不为空,则访问本地保存的数据
Log.i(TAG,shared+"");
msg.what=SHARED_REDEY;
handler.sendMessage(msg);
dialog.dismiss();
}
}catch(JSONException e){
Log.i(TAG,"JSONException");
e.printStackTrace();
}catch(Exception e){
Log.i(TAG,"Exception");
e.printStackTrace();
}
}
});
其中WebAccessTools()方法,
/**
* @网站访问工具类,用于天气信息的网络访问
*/
public class WebAccessTools {
private static final String TAG = "WcbAccessTools";
/**
* 根据给定的url地址访问网络,得到响应内容(这里为GET方式访问)
* @param path 指定的url地址
* @return web服务器响应的内容,为<code>String</code>类型,当访问失败时,返回为null
*/
public String getHtml(String path){
try{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(15 * 1000); // 别超过15秒
System.out.println(conn.getResponseCode());//获取网络返回的状态
if(conn.getResponseCode()==200){ //如果成功,则返回数据流
InputStream inputStream=conn.getInputStream();
byte[] data=readStream(inputStream);
return new String(data);
}
}catch(SocketTimeoutException e){
Log.i(TAG,"SocketTimeoutException");
e.printStackTrace();
} catch (IOException e) {
Log.i(TAG,"IOException");
e.printStackTrace();
} catch (Exception e) {
Log.i(TAG,"Exception");
e.printStackTrace();
} return null;
}
/**
* 读取网络数据流
* @param inStream 指定的网络数据流
* @return web服务器响应的字节内容,为byte[]数组类型
*/
public static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outstream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=-1;
while((len=inStream.read(buffer)) !=-1){
outstream.write(buffer, 0, len);
}
outstream.close();
inStream.close();
return outstream.toByteArray();
}
}
接下来,启动线程之后,同样定义一个handler获取信息。
handler=new Handler(){
@Override//当网络JSON数据准备好了,发送消息
public void handleMessage(Message msg){
switch(msg.what){
case JSONDATA_REDEY:
Toast.makeText(WeatherActivity.this, "天气资讯更新成功!", Toast.LENGTH_SHORT).show();
setWeatherSituation(json);
break;
case SHARED_REDEY:
setWeatherSituation(shared);
break;
// case GETWEATHER_FAILED:
// Toast.makeText(WeatherActivity.this, "不好,联结网络失败!", Toast.LENGTH_SHORT).show();
//dialog.dismiss();
}
}
};
相对应的 setWeatherSituation(json) 方法:
//由根据城市码获取网络的天气情况,并将得到的信息保存在文件中
public void setWeatherSituation(JSONObject json) {
Log.i(TAG,"enter setWeatherSituation--Json");
try {
TextView tempText = null;
ImageView imageView=null;
int weather_icon = 0;
//建立一个缓存天气的文件
SharedPreferences.Editor editor = getSharedPreferences(STORE_WEATHER, MODE_PRIVATE).edit();
//得到城市
String info = json.getString("city");
tempText=(TextView)findViewById(R.id.cityField);
tempText.setText(info);
editor.putString("city", info);
//得到日期
info= json.getString("date_y") ;
tempText=(TextView)findViewById(R.id.date_y);
tempText.setText(info);
editor.putString("date_y", info);
//得到星期
info= json.getString("week");
tempText=(TextView)findViewById(R.id.week);
tempText.setText(info);
editor.putString("week", info);
//得到温度
info= json.getString("temp1");
tempText=(TextView)findViewById(R.id.currentTemp);
tempText.setText(info);
editor.putString("temp1", info);
//得到天气
info= json.getString("weather1");
tempText=(TextView)findViewById(R.id.currentWeather);
tempText.setText(info);
editor.putString("weather1", info);
//天气图标
info= json.getString("img_title1");
imageView=(ImageView)findViewById(R.id.weather_icon01);
weather_icon = getWeatherBitMapResource(info);
imageView.setImageResource(weather_icon);
editor.putInt("img_title1", weather_icon);
//得到风向
info= json.getString("wind1");
tempText=(TextView)findViewById(R.id.currentWind);
tempText.setText(info);
editor.putString("wind1", info);
//得到建议
info= json.getString("index_d");
tempText=(TextView)findViewById(R.id.index_d);
tempText.setText(info);
editor.putString("index_d", info);
//得到明天的天气
info= json.getString("weather2");
tempText=(TextView)findViewById(R.id.weather02);
tempText.setText(info);
editor.putString("weather2", info);
//明天的图标
info= json.getString("img_title2");
imageView=(ImageView)findViewById(R.id.weather_icon02);
weather_icon = getWeatherBitMapResource(info);
imageView.setImageResource(weather_icon);
editor.putInt("img_title2", weather_icon);
//明天的气温
info= json.getString("temp2");
tempText=(TextView)findViewById(R.id.temp02);
tempText.setText(info);
editor.putString("temp2", info);
//明天的风力
info= json.getString("wind2");
tempText=(TextView)findViewById(R.id.wind02);
tempText.setText(info);
editor.putString("wind2", info);
//后天的天气
info= json.getString("weather3");
tempText=(TextView)findViewById(R.id.weather03);
tempText.setText(info);
editor.putString("weather3", info);
//后天天气图标
info= json.getString("img_title3");
imageView=(ImageView)findViewById(R.id.weather_icon03);
weather_icon = getWeatherBitMapResource(info);
imageView.setImageResource(weather_icon);
editor.putInt("img_title3", weather_icon);
//后天的气温
info= json.getString("temp3");
tempText=(TextView)findViewById(R.id.temp03);
tempText.setText(info);
editor.putString("temp3", info);
//后天的风力
info= json.getString("wind3");
tempText=(TextView)findViewById(R.id.wind03);
tempText.setText(info);
editor.putString("wind3", info);
//设置一个有效日期为1小时
//long validTime = System.currentTimeMillis();
//validTime = validTime + 1*60*60*1000;
//editor.putLong("validTime", validTime);
//提交,保存
editor.commit();
}catch(Exception e) {
Toast.makeText(WeatherActivity.this,"不好,获取网络数据出错了!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
顺便这里讲一下SharedPreferences的用法,
先定义
final SharedPreferences shared = getSharedPreferences(STORE_WEATHER, MODE_PRIVATE);
再建立一个editor
SharedPreferences.Editor editor = getSharedPreferences(STORE_WEATHER, MODE_PRIVATE).edit();
editor.putString("weather1", info);
editor.commit();
相对应访问本地数据setWeatherSituation(shared);
//通过本地缓存文件来得到天气情况
public void setWeatherSituation(SharedPreferences shared) {
Log.i(TAG,"enter setWeatherSituation--shared");
String info = null;
TextView tempText = null;
ImageView imageView=null;
//得到城市
info = shared.getString("city", "");
tempText=(TextView)findViewById(R.id.cityField);
tempText.setText(info);
//得到日期
info= shared.getString("date_y", "");
tempText=(TextView)findViewById(R.id.date_y);
tempText.setText(info);
//得到星期
info= shared.getString("week", "");
tempText=(TextView)findViewById(R.id.week);
tempText.setText(info);
//得到温度
info= shared.getString("temp1", "");
tempText=(TextView)findViewById(R.id.currentTemp);
tempText.setText(info);
//得到天气
info= shared.getString("weather1", "");
tempText=(TextView)findViewById(R.id.currentWeather);
tempText.setText(info);
//天气图标
imageView=(ImageView)findViewById(R.id.weather_icon01);
imageView.setImageResource(shared.getInt("img_title1", 0));
//得到风向
info= shared.getString("wind1", "");
tempText=(TextView)findViewById(R.id.currentWind);
tempText.setText(info);
//得到建议
info= shared.getString("index_d", "");
tempText=(TextView)findViewById(R.id.index_d);
tempText.setText(info);
//得到明天的天气
info= shared.getString("weather2", "");
tempText=(TextView)findViewById(R.id.weather02);
tempText.setText(info);
//明天的图标
imageView=(ImageView)findViewById(R.id.weather_icon02);
imageView.setImageResource(shared.getInt("img_title2", 0));
//明天的气温
info= shared.getString("temp2", "");
tempText=(TextView)findViewById(R.id.temp02);
tempText.setText(info);
//明天的风力
info= shared.getString("wind2", "");
tempText=(TextView)findViewById(R.id.wind02);
//后天的天气
info= shared.getString("weather3", "");
tempText=(TextView)findViewById(R.id.weather03);
tempText.setText(info);
//后天天气图标
imageView=(ImageView)findViewById(R.id.weather_icon03);
imageView.setImageResource(shared.getInt("img_title3", 0));
//后天的气温
info= shared.getString("temp3", "");
tempText=(TextView)findViewById(R.id.temp03);
tempText.setText(info);
//后天的风力
info= shared.getString("wind3", "");
tempText=(TextView)findViewById(R.id.wind03);
tempText.setText(info);
}
安卓市场:http://apk.hiapk.com/html/2013/05/1468059.html?module=256&info=IWjtVg9cqVJLYg%3D%3D
N多市场:http://www.nduoa.com/apk/detail/553415
360手机助手:http://zhushou.360.cn/search/index/?kw=%E6%A0%A1%E5%9B%AD%E5%B0%8F%E5%8A%A9%E6%89%8B
百度应用:http://as.baidu.com/a/item?docid=3101724&pre=web_am_se
优亿市场(eoe):http://www.eoemarket.com/search/apps/?keyword=%E6%A0%A1%E5%9B%AD%E5%B0%8F%E5%8A%A9%E6%89%8B
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)