Fastjson获取天气信息封装bean

天气预报接口网上一艘一大堆。。

基本是国家天气预报的和google 啊 雅虎啊等。

给个示例地址:http://blog.21004.com/post/178.html

还有和json在线格式化地址:http://tmxk.org/c/json/ 

然后就是FastJson 地址:http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=2424946

关于Fastjson是干什么的,可以百度,简单解释就是把 json字符串封装为bean 或者吧bean 封装成 json 方便快读填充 。


 

正文:

获取天气,封装了一个类,根据请求地址返回天气信息,代码:

package com.wangyyworks.weatherworks;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import utils.NetUtil;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wangyyworks.weatherworks.R.string;

/**
 * 定时从网络获取天气信息,单例使用。 2013-6-7 21:02:16 测试通过
 * 
 * @author wang_xiaohao
 * 
 */
public class WeatherInfoProvider {

    private static final String TAG = "WeatherInfoProvider";
    public String weatheruri = "http://m.weather.com.cn/data/101010100.html";// 天气预报的uir
    private static Context mContext;
    private String fastjsonkey = "weatherinfo";// 用来设置json返回主句后获得Object
                                                // 然后获得天气的bean 不然不能填充数据;

    private WeatherInfo weatherinfo;// 用来封装请求返回数据的bean;

    private WeatherInfoProvider() {
    };

    private static WeatherInfoProvider weatherinfoprovider;

    /**
     * 单例模式
     * 
     * @param context
     *            需要传递上下文
     * @return
     */
    public static WeatherInfoProvider getInstance(Context context) {
        mContext = context;
        if (weatherinfoprovider == null) {
            weatherinfoprovider = new WeatherInfoProvider();
        }
        return weatherinfoprovider;
    }

    /**
     * 根据uri请求获取天气信息返回
     * 
     * @param uri
     *            请求天气的uri 代码
     * @return 返回此uri天气信息
     */
    private WeatherInfo getWeatherInfo(String weatheruri) {

        // 使用nettuils访问uri 然后封装返回信息;
        // 判断网络是否正常
        if (NetUtil.checkNet(mContext)) {

            // 访问网络,返回获取后的天气信息bean
            WeatherInfo weatherinfo = httpClientConn(weatheruri);

            // TODO接下来,可以传递进行匹配展示
        }
        return null;
    }

    /**
     * 根据需要请求的地址 返回请求信息封装bean
     * 使用HttpCient连接到国家天气网站 信息 地址:http://m.weather.com.cn/data/101010100.html
     * @param weatheruri
     *            需要请求城市地址
     * @return 返回请求城市天气信息的bean 如果为null表示请求失败
     */
    protected WeatherInfo httpClientConn(String weatheruri) {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(weatheruri);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        try {
            String content = httpclient.execute(httpget, responseHandler);
            Log.i(TAG, "连接服务器成功!");
            // 设置TextView
            Log.i(TAG, content);
            // 开始填充数据
            content = content.substring(content.indexOf(":") + 1,
                    content.lastIndexOf("}"));// 截取需要的部分、不包含{"weatherinfo":和结尾的}
            Log.i(TAG, "测试获得字符串下面继续解析" + content);
            weatherinfo = JSON.parseObject(content, WeatherInfo.class);
            Log.i(TAG, "weatherinfo  tostring" + weatherinfo.toString());

        } catch (Exception e) {
            Log.i(TAG, "连接失败。。。。。");
            e.printStackTrace();
        } finally {
            // 关闭网络连接
            httpclient.getConnectionManager().shutdown();
        }
        return weatherinfo;
    }
}

用到一个工具类,检查手机是否联网,没细研究 ,NetUtil代码:

 

 1 package utils;
 2 
 3 import android.content.ContentResolver;
 4 import android.content.Context;
 5 import android.database.Cursor;
 6 import android.net.ConnectivityManager;
 7 import android.net.NetworkInfo;
 8 import android.net.Uri;
 9 
10 public class NetUtil {
11     private static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
12 
13     /**
14      * 获取当前手机端的联网方式 一旦检测到当前用户使用的是wap方式,设置ip和端口
15      */
16     public static boolean checkNet(Context context) {
17         boolean wifiConnectivity = isWIFIConnectivity(context);
18         boolean mobileConnectivity = isMobileConnectivity(context);
19 
20         if (wifiConnectivity == false && mobileConnectivity == false) {
21             // 需要配置网络
22             return false;
23         }
24 
25         // mobile(APN)
26         if (mobileConnectivity) {
27             // wap方式时端口和ip
28             // 不能写死:IP是10.0.0.172 端口是80
29             // 一部分手机:IP是010.000.000.172 端口是80
30             // 读取当亲的代理信息
31             readApn(context);
32         }
33         return true;
34     }
35 
36     /**
37      * 读取ip和端口信息
38      * 
39      * @param context
40      */
41     private static void readApn(Context context) {
42         ContentResolver resolver = context.getContentResolver();
43         Cursor query = resolver.query(PREFERRED_APN_URI, null, null, null, null);// 获取手机Apn:获取当前处于激活状态的APN信息
44         if (query != null && query.moveToFirst()) {
45             GloableParams.PROXY_IP = query.getString(query.getColumnIndex("proxy"));
46             GloableParams.PROXY_PORT = query.getInt(query.getColumnIndex("port"));
47         }
48 
49     }
50 
51     /**
52      * WIFI是否连接
53      * 
54      * @param context
55      * @return
56      */
57     public static boolean isWIFIConnectivity(Context context) {
58         ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
59         NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
60         if (info != null)
61             return info.isConnected();
62         return false;
63     }
64 
65     /**
66      * 手机APN是否连接
67      * 
68      * @param context
69      * @return
70      */
71     public static boolean isMobileConnectivity(Context context) {
72         ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
73         NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
74         if (info != null)
75             return info.isConnected();
76         return false;
77     }
78 
79 }

 

基本就些了。

WeatherInfo weatherinfo = httpClientConn(weatheruri);

返回的json数据封装到了weatherinfo,这个类中全部是i常量,贴出来没意义,写这个类时候用了notepad++ 的替换,不然要吐血。。

接下来,构思下数据保存和服务,布局。

两天能写出来?额,加油,骚年。

想获取一天中的温度变化,接口不提供,悲催。。

做饭去,饿了。。。。

格式化了下封装好的结果,看起来舒服多了:

06-07 21:27:24.707: 
I/WeatherInfoProvider(14257):
 weatherinfo  tostringWeatherInfo 
 [
     city=北京
     city_en=beijing
     date_y=2013年6月7日
     date=
     week=星期五
     fchh=18
     cityid=101010100
     temp1=19℃~23℃
     temp2=18℃~22℃
     temp3=16℃~23℃
     temp4=14℃~29℃
     temp5=19℃~30℃
     temp6=20℃~29℃
     tempF1=66.2℉~73.4℉
     tempF2=64.4℉~71.6℉
     tempF3=60.8℉~73.4℉
     tempF4=57.2℉~84.2℉
     tempF5=66.2℉~86℉
     tempF6=68℉~84.2℉
     weather1=阵雨转雷阵雨
     weather2=大雨转小雨
     weather3=阴转阵雨
     weather4=多云转晴
     weather5=晴
     weather6=多云
     img1=3
     img2=4
     img3=9
     img4=7
     img5=2
     img6=3
     img7=1
     img8=0
     img9=0
     img10=99
     img11=1
     img12=99
     img_single=4
     img_title1=阵雨
     img_title2=雷阵雨
     img_title3=大雨
     img_title4=小雨
     img_title5=阴
     img_title6=阵雨
     img_title7=多云
     img_title8=晴
     img_title9=晴
     img_title10=晴
     img_title11=多云
     img_title12=多云
     img_title_single=雷阵雨
     wind1=微风
     wind2=微风
     wind3=微风
     wind4=微风
     wind5=微风
     wind6=微风
     fx1=微风
     fx2=微风
     fl1=小于3级
     fl2=小于3级
     fl3=小于3级
     fl4=小于3级
     fl5=小于3级
     fl6=小于3级
     index=较舒适
     index_d=建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。
     index48=较舒适
     index48_d=建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。
     index_uv=最弱
     index48_uv=最弱
     index_xc=不宜
     index_tr=一般
     index_co=舒适
     st1=21
     st2=17
     st3=20
     st4=16
     st5=21
     st6=15
     index_cl=较不宜
     index_ls=不宜
     index_ag=极不易发
]

 

回见~

转载于:https://www.cnblogs.com/wang-xiaohao/archive/2013/06/07/3125313.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值