流水不争先,争的是滔滔不绝

模仿android手机系统天气预报(源码)

IM源码 macgrady 140℃

模仿android手机系统天气预报

即时通讯IM发展到今天,不但在通讯效率和安全方面有着飞速发展与保障,在日常应用中也会给用户提供各种便捷服务。今天我们来聊一聊天气预报功能在android中的简单实现.本团队多年来一直专注于即时通讯软件开发,IM产品多项测试下安全稳定,可按需求定制开发。适配兼容各种常用手机设备,支持Android,ios,ipad端口,独立自主私有部署管理,不依赖第三方,压力测试1000小时无异常,界面操作仿微信简单方便,新老用户易上手操作,全部源码低价出售。详情咨询

数据存储采用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的解读相对来说也是没有什么难点,都是封装好的东西。

 textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);

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

 主要的布局方式说明:

 

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

 

 免费下载,献上源码地址:

 

http://download.csdn.net/detail/wduj123/9508500

————————————————

版权声明:本文为CSDN博主「温讯春风」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/wduj123/article/details/51302357

版权声明:部分文章、图片等内容为用户发布或互联网整理而来,仅供学习参考。如有侵犯您的版权,请联系我们,将立刻删除。
点击这里给我发消息