使用网络技术-天气预报

一 效果图


二 核心代码

1 MainActivity.java

package com.example.weather;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
  
public class MainActivity extends Activity  
{  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_weather);  
    }  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu)  
    {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);  
        return true;  
    }  
} 

WeatherActivity.java

package com.example.weather;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
  
public class WeatherActivity extends Activity  
{  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_weather);  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu)  
    {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.weather, menu);  
        return true;  
    }  
  
}  
3.WeatherGetTest.java

package com.example.weather;  
  
import com.example.weather.util.HttpCallbackListener;  
import com.example.weather.util.HttpUtil;  
  
import android.test.AndroidTestCase;  
  
public class WeatherGetTest extends AndroidTestCase  
{  
    public void testGetData(){  
        String weatherUrl="http://v.juhe.cn/weather/index?format=2&cityname= 滨 州&key=aaaeccabc5bdfb201d21854d0ce3fdd7";  
        HttpUtil.sendHttpRequest(weatherUrl, new HttpCallbackListener() {  
        public void onFinish(String response) {  
        System.out.println(response);  
        }   
        public void onError(Exception e) {  
        }  
        });  
        }  
}  
4.WeatherAdapter.java

package com.example.weather.adapter;  
  
import java.util.List;  
  
import com.example.weather.R;  
import com.example.weather.model.Weather;  
  
import android.content.Context;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.ArrayAdapter;  
import android.widget.TextView;  
  
public class WeatherAdapter extends ArrayAdapter<Weather>  
{  
    private int resourceId;  
  
    public WeatherAdapter(Context context, int textViewResourceId,  
            List<Weather> objects)  
    {  
        super(context, textViewResourceId, objects);  
        resourceId = textViewResourceId;  
    }  
  
    public View getView(int position, View convertView, ViewGroup viewgroup)  
    {  
        Weather weather = getItem(position);  
        ViewHolder viewHolder = null;  
        if (convertView == null)  
        {  
            viewHolder = new ViewHolder();  
            convertView = LayoutInflater.from(getContext()).inflate(resourceId,  
                    null);  
            viewHolder.tvDayOfWeek = (TextView) convertView  
                    .findViewById(R.id.action_settings);  
            viewHolder.tvDate = (TextView) convertView  
                    .findViewById(R.id.action_settings);  
            viewHolder.tvTemperature = (TextView) convertView  
                    .findViewById(R.id.action_settings);  
            viewHolder.tvWeather = (TextView) convertView  
                    .findViewById(R.id.action_settings);  
            convertView.setTag(viewHolder);  
        } else  
        {  
            viewHolder = (ViewHolder) convertView.getTag();  
        }  
        viewHolder.tvDayOfWeek.setText(weather.getDayOfWeek());  
        viewHolder.tvDate.setText(weather.getDate());  
        viewHolder.tvTemperature.setText(weather.getTemperature());  
        viewHolder.tvWeather.setText(weather.getWeather());  
        return convertView;  
    }  
  
    private class ViewHolder  
    {  
        TextView tvDayOfWeek;  
        TextView tvDate;  
        TextView tvTemperature;  
        TextView tvWeather;  
    }  
}
5.Weather.java

package com.example.weather.model;  
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;  
    }  
  
    public String toString()  
    {  
        return "Weather [dayOfWeek=" + dayOfWeek + ", date=" + date  
                + ", temperature=" + temperature + ", weather=" + weather + "]";  
    }  
}  

6.HttpCallbackListener.java

package com.example.weather.util;  
  
public interface HttpCallbackListener  
{  
    void onFinish(String response);  
    void onError(Exception e);  
}  

7.HttpUtil.java

package com.example.weather.util;  
  
  
import java.io.BufferedReader;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.net.HttpURLConnection;  
import java.net.URL;  
  
  
public class HttpUtil  
{  
<span style="white-space:pre">  </span>public static void sendHttpRequest(final String address,  
<span style="white-space:pre">          </span>final HttpCallbackListener listener)  
<span style="white-space:pre">  </span>{  
<span style="white-space:pre">      </span>new Thread(new Runnable()  
<span style="white-space:pre">      </span>{  
<span style="white-space:pre">          </span>@Override  
<span style="white-space:pre">          </span>public void run()  
<span style="white-space:pre">          </span>{  
<span style="white-space:pre">              </span>HttpURLConnection connection = null;  
<span style="white-space:pre">              </span>try  
<span style="white-space:pre">              </span>{  
<span style="white-space:pre">                  </span>URL url = new URL(address);  
<span style="white-space:pre">                  </span>connection = (HttpURLConnection) url.openConnection();  
<span style="white-space:pre">                  </span>connection.setRequestMethod("GET");  
<span style="white-space:pre">                  </span>connection.setConnectTimeout(8000);  
<span style="white-space:pre">                  </span>connection.setReadTimeout(8000);  
<span style="white-space:pre">                  </span>connection.setDoInput(true);  
<span style="white-space:pre">                  </span>connection.setDoOutput(true);  
<span style="white-space:pre">                  </span>InputStream in = connection.getInputStream();  
<span style="white-space:pre">                  </span>BufferedReader reader = new BufferedReader(  
<span style="white-space:pre">                          </span>new InputStreamReader(in));  
<span style="white-space:pre">                  </span>StringBuilder response = new StringBuilder();  
<span style="white-space:pre">                  </span>String line;  
<span style="white-space:pre">                  </span>while ((line = reader.readLine()) != null)  
<span style="white-space:pre">                  </span>{  
<span style="white-space:pre">                      </span>response.append(line);  
<span style="white-space:pre">                  </span>}  
<span style="white-space:pre">                  </span>if (listener != null)  
<span style="white-space:pre">                  </span>{  
<span style="white-space:pre">                      </span>// 回调 onFinish()方法  
<span style="white-space:pre">                      </span>listener.onFinish(response.toString());  
<span style="white-space:pre">                  </span>}  
<span style="white-space:pre">              </span>} catch (Exception e)  
<span style="white-space:pre">              </span>{  
<span style="white-space:pre">                  </span>if (listener != null)  
<span style="white-space:pre">                  </span>{  
<span style="white-space:pre">                      </span>// 回调 onError()方法  
<span style="white-space:pre">                      </span>listener.onError(e);  
<span style="white-space:pre">                  </span>}  
<span style="white-space:pre">              </span>} finally  
<span style="white-space:pre">              </span>{  
<span style="white-space:pre">                  </span>if (connection != null)  
<span style="white-space:pre">                  </span>{  
<span style="white-space:pre">                      </span>connection.disconnect();  
<span style="white-space:pre">                  </span>}  
<span style="white-space:pre">              </span>}  
<span style="white-space:pre">          </span>}  
<span style="white-space:pre">      </span>}).start();  
<span style="white-space:pre">  </span>}  
}  

8.weather_list_animation.xml

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">  
<span style="white-space:pre">  </span><scale  
<span style="white-space:pre">      </span>android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
<span style="white-space:pre">      </span>android:fromXScale="0.0"  
<span style="white-space:pre">      </span>android:toXScale="1.0"  
<span style="white-space:pre">      </span>android:fromYScale="0.0"  
<span style="white-space:pre">      </span>android:toYScale="1.0"  
<span style="white-space:pre">      </span>android:duration="1000"  
<span style="white-space:pre">  </span>/><span style="white-space:pre">  </span>  
</set>

9.weather_list_layout_animation.xml 

<?xml version="1.0" encoding="utf-8"?>  
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"  
    android:animation="@anim/weather_list_animation"  
    android:animationOrder="normal"  
    android:delay="2" />  

10.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> 

11.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/activity_weather_bg"  
    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/icons_weather_city"  
            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/icons_weather_query" />  
    </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>  
  
  
</RelativeLayout> 

三 总结


 学会了利用SQLite数据库来完成程序设计

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值