您的位置:首页 > 移动开发 > Android开发

【项目源码】- 【天气预报】模仿魅族系统天气预报android

2016-05-03 10:56 543 查看
这个天气预报主要学习下网络方面的东西,比如json数据的读取,了解网络的一些知识。

其中还涉及的知识点包括:

 动画的操作,如入云的移动用的translateAnimation ,

还有 assert中txt文本的读取,

listview中的按钮点击事件,

scrollview的一些拖动的操作锁定边界的一些操作,

文字大小动作变化等等。

效果图如下:









数据存储采用SharedPreference,内容还不够完善,定位没有处理,预警信息没有,主要的接口信息只有一个。

采用http请求代码如下:package com.weather.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.weather.activity.MainActivity;
import com.weather.bean.WeatherInfo;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

/**
* Created by Administrator on 2016/4/19.
*/
public class HttpUtils{

public HttpUtils(){
}

public static String getWeatherJsonData(String path){
if(TextUtils.isEmpty(path)) {
Log.e("提示信息:", "路径不能为空");
return null;
}else{
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; Shuame)");
int code = conn.getResponseCode();
if (code == 200) {
String data = changeInputStreamToString(conn.getInputStream());
return data;
}else{
Log.e("提示信息:", "连接失败");
return null;
}
}catch (Exception e){
e.printStackTrace();
Log.e(e.toString(), "获取失败");
return null;
}
}
}

/**
*
* @param is 输入流
* @return 返回转换后的字符串
*/
public static String changeInputStreamToString(InputStream is){
BufferedReader bufr = new BufferedReader(new InputStreamReader(is));
try {
String line = null;
StringBuilder sb = new StringBuilder();
while((line = bufr.readLine())!=null){
sb.append(line);
}
//System.out.println(sb.toString());
return sb.toString();
}catch (Exception e){
e.printStackTrace();
Log.e(e.getMessage(), "转换失败");
return null;
}finally {
try {
is.close();
}catch (Exception e){
e.printStackTrace();
Log.e(e.getMessage(),"关闭失败");
}
}
}
}

接口采用:http://wthrcdn.etouch.cn/weather_mini?citykey=101010100

101010100为城市的代码。

发送http请求,得到json数据

得到json数据后再进行解析得到数据bean

代码如下:

package com.weather.utils;

import android.util.Log;

import com.weather.bean.DayWeatherInfo;
import com.weather.bean.WeatherInfo;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Administrator on 2016/4/19.
*/
public class JsonUtils {

private String city;//城市名
private String aqi;//城市id
private String wendu;//温度
private String ganmao;//风向
private String forecast;//各星期数据
private String yesterday;//昨天数据

public static WeatherInfo getWeatherInfo(String key,String jsonData){
try {
JSONObject jsonObject = new JSONObject(jsonData);
JSONObject jsonWeather = jsonObject.getJSONObject(key);

WeatherInfo weatherInfo = new WeatherInfo();
weatherInfo.setCity(jsonWeather.getString("city"));
//weatherInfo.setAqi(jsonWeather.getString("aqi"));//有些没有此项数据
weatherInfo.setGanmao(jsonWeather.getString("ganmao"));
weatherInfo.setWendu(jsonWeather.getString("wendu"));

JSONArray jsonDayWeahers = jsonWeather.getJSONArray("forecast");
List<DayWeatherInfo> dayWeatherInfos = new ArrayList<DayWeatherInfo>();
for(int i=0;i<jsonDayWeahers.length();i++){
JSONObject obj = jsonDayWeahers.getJSONObject(i);
DayWeatherInfo dayWeatherInfo = new DayWeatherInfo();
/* [{"fengxiang":"无持续风向","fengli":"微风级","high":"高温 15℃","type":"阴","low":"低温 9℃","date":"19日星期二"},*/
dayWeatherInfo.setFengxiang(obj.getString("fengxiang"));
dayWeatherInfo.setFengli(obj.getString("fengli"));
dayWeatherInfo.setHigh(obj.getString("high"));
dayWeatherInfo.setLow(obj.getString("low"));
dayWeatherInfo.setType(obj.getString("type"));
dayWeatherInfo.setDate(obj.getString("date"));
dayWeatherInfos.add(dayWeatherInfo);
}
weatherInfo.setForecast(dayWeatherInfos);

JSONObject jsonObject1 = jsonWeather.getJSONObject("yesterday");
/*{"fl":"3-4级","fx":"北风","high":"高温 21℃","type":"晴","low":"低温 8℃","date":"18日星期一"},*/
DayWeatherInfo dayWeatherInfo = new DayWeatherInfo();
dayWeatherInfo.setFengxiang(jsonObject1.getString("fx"));
dayWeatherInfo.setFengli(jsonObject1.getString("fl"));
dayWeatherInfo.setHigh(jsonObject1.getString("high"));
dayWeatherInfo.setLow(jsonObject1.getString("low"));
dayWeatherInfo.setType(jsonObject1.getString("type"));
dayWeatherInfo.setDate(jsonObject1.getString("date"));
weatherInfo.setYesterday(dayWeatherInfo);

return weatherInfo;
}catch (Exception e){
e.printStackTrace();
Log.e(e.toString(),"获取Json数据失败");
return null;
}
}
}

json数据的读取应该也没有什么难点,之前采用那个晚上推荐的天气接口一直读取不到数据,采用上面那个接口问题就解决了,不过读取的数据比较少,不过对于刚接触网络数据来说就可以了,了解的数据的一些基本处理知识,对于json的解读相对来说也是没有什么难点,都是封装好的东西。

a416

现在说说作者在做这个天气预报中所遇到的几个难点:

第一个是,文字大小的变化,这程序中有一个拖动界面改变文字大小的操作,作者采用直接读取getTextSize()的方法,然后在用这个值去设置TextView的高度,结果出现了文字高度变大的问题,卡了一些时间,后来问了下同事,解决了问题

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);

采用TypedValue.COMPLEX_UNIT_PX这个参数后问题就解决

第二个难点就是拖动时的位置吸引问题,但拖动时会自动移到某一位置的操作,主要采用的是OnTouch方法,不过这个方法根ScrollView又有点冲突,所以在移出范围时停止这个监听方法设置为null 在范围内时在设置,这样就解决了冲突的问题。

第三个难点在于城市 名列表,不过作者还没有实现 城市名的查找问题,只是实现了列表的排序问题,实现的方法是 先下载一份城市和id的txt放入Asserts当中,这样才启动的时候 放入List集合但中,当然读取txt的文件比较久,要放入到子线程当中去。然后在往集合当中加入英文大写字母集合,然后进行排序。关于中文转换成拼音的方法,采用网上大神的方法。

主要的布局方式说明:

主要是一个ScrollView嵌套一个Viewpager这样就实现了既可以左右滚动和上下移动的体验,具体实现方法看源码中。

免费下载,献上源码地址:
http://download.csdn.net/detail/wduj123/9508500
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息