实验5:使用网络技术-天气预报

1 实验目的

输入要查询的城市名称,点击查询按钮后,依次出现七天的天气情况。

2 实验过程。

1、首先搞定天气预报数据来源的问题,提高天气预报服务的有很多网站,这些网站一般都会提供比较详细的 API 接口供应用程序调用。

3 代码部分。

activity_weather.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/city"
    tools:context=".WeatherActivity" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/etCity"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:layout_weight="1"
            android:background="@android:drawable/edit_text"
            android:drawableLeft="@drawable/etcity"
            android:drawablePadding="5dp"
            android:ems="10"
            android:hint="@string/etCity" >

            <requestFocus />
        </EditText>

        <ImageButton
            android:id="@+id/btnQuery"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"      
            android:background="@null"
            android:src="@drawable/serch" />
    </LinearLayout>

    <ListView
        android:id="@+id/lvFutureWeather"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:dividerHeight="10dp"
        android:layoutAnimation="@anim/weather_list_layout_animation" >
    </ListView>

<span style="color:#ff0000;"></RelativeLayout></span>


activity_weather_listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/list_item_shape"
    android:padding="10dp" >

    <TextView
        android:id="@+id/tvDayofWeek"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:text="星期日" />

    <TextView
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/tvDayofWeek"
        android:layout_alignBottom="@+id/tvDayofWeek"
        android:layout_alignParentRight="true"
        android:text="20160207" />

    <TextView
        android:id="@+id/tvTemperature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvDayofWeek"
        android:layout_below="@+id/tvDayofWeek"
        android:layout_marginTop="15dp"
        android:text="temperature" />

    <TextView
        android:id="@+id/tvWeather"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvTemperature"
        android:layout_below="@+id/tvTemperature"
        android:layout_marginTop="15dp"
        android:text="weather" />

</RelativeLayout>

Weather.java

package com.example.weather.moder;

public class Weather {
	private String dayOfWeek;//星期几
	private String date;//日期
	private String temperature;//温度
	private String weather;//天气
	public Weather(){
	}
	public Weather(String dayOfWeek, String date, String temperature,
			String weather) {
		super();
		this.dayOfWeek = dayOfWeek;
		this.date = date;
		this.temperature = temperature;
		this.weather = weather;
	}
	public String getDayOfWeek() {
		return dayOfWeek;
	}
	public void setDayOfWeek(String dayOfWeek) {
		this.dayOfWeek = dayOfWeek;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getTemperature() {
		return temperature;
	}
	public void setTemperature(String temperature) {
		this.temperature = temperature;
	}
	public String getWeather() {
		return weather;
	}
	public void setWeather(String weather) {
		this.weather = weather;
	}
	@Override
	public String toString() {
		return "Weather [dayOfWeek=" + dayOfWeek + ", date=" + date
				+ ", temperature=" + temperature + ", weather=" + weather + "]";
	}

}

4 实验注意事项:在进行查询输入城市名时一定要先改编码格式  列如
<span style="font-size:18px;">cityName = URLEncoder.encode("滨州", "utf-8");</span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值