天气预报
2016-12-04 18:03
218 查看
一 UI效果
二 核心代码
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;
}
}
2.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
二 核心代码
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;
}
}
2.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 { public static void sendHttpRequest(final String address, final HttpCallbackListener listener) { new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL(address); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); connection.setDoInput(true); connection.setDoOutput(true); InputStream in = connection.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(in)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } if (listener != null) { // 回调 onFinish()方法 listener.onFinish(response.toString()); } } catch (Exception e) { if (listener != null) { // 回调 onError()方法 listener.onError(e); } } finally { if (connection != null) { connection.disconnect(); } } } }).start(); } }
8.weather_list_animation.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:duration="1000" /> </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>
相关文章推荐
- 在blog中显示天气预报、日历、时钟、MSN、QQ在线状态
- 新年新气象,学习Bonny,wang设置了首页时钟和天气预报
- CSDN blog 添加音乐和天气预报效果
- 新浪天气预报代码及城市代码
- 观察:若干现有的RSS天气预报服务
- ASP.NET的实时天气及24小时天气预报(C#)
- 天气预报
- Linux下的语音天气预报脚本
- Java获取Google的简单天气预报
- 使用Google Weather API查询天气预报
- 获取天气预报功能
- 免费天气预报插件jquery版本
- webservice简单应用(天气预报)
- 自制 手机天气预报
- 部分天气预报脚本
- 【实践】基于百度地图API实现的地图形式天气预报【二】
- 网站中嵌入免费天气预报
- 天气预报程序第三篇(完结)
- 天气预报接口(中国天气网)
- 天气预报接口(中国天气网)