手记

近期用到的Xutils和Gson

好久没有用Xutils网络请框架了,一直在用原生态,感觉写起来特别长,前段时间也用了Okhttp感觉还是不错的

 

说实话其实第三方的框架只要会一个其他的都不会难的,只要去研究一下怎么用比较合适

 

下面以Xutils为例:废话不多说,直接上代码,xutils有个不好的地方就是6.0后xutlis3.0一下不能用

 

先上字符串:

{

    "areaInfo": {

        "AQI": "45",

        "AQILevel": "2",

        "areaName": "丰台区",

        "highTemperature": "22",

        "lowTemperature": "13",

        "nowTemperature": "17",

        "weatherState": "阴",

        "windDirect": "东风",

        "windPower": "3级"

    },

    "code": "0",

    "msg": "查询扬尘信息成功。",

    "pointListInfo": [

        {

            "PM10": 80,

            "PM25": 19,

            "noise": 0.0,

            "pointId": "0f3d4260-ec2a-4dec-9c11",

            "pointName": "丰西商砼",

            "recTime": "2016-09-28 13:00:00"

        },

        {

            "PM10": 86,

            "PM25": 19,

            "noise": 0.0,

            "pointId": "3a78d0fa-8917-4f3f-9137",

            "pointName": "亿和混凝土有限公司",

            "recTime": "2016-09-28 13:00:00"

        },

        {

            "PM10": 71,

            "PM25": 17,

            "noise": 0.0,

            "pointId": "6b3d1929-7b15-4993-847",

            "pointName": "物配建材",

            "recTime": "2016-09-28 13:00:00"

        },

        {

            "PM10": 80,

            "PM25": 15,

            "noise": 0.0,

            "pointId": "89445268-84d1-4374-a10f",

            "pointName": "安泰商品混凝土",

            "recTime": "2016-09-28 13:00:00"

        },

        {

            "PM10": 81,

            "PM25": 16,

            "noise": 0.0,

            "pointId": "9ec45f60-2c37-44f7-9bf7",

            "pointName": "众邦混凝土",

            "recTime": "2016-09-28 13:00:00"

        }

    ]

}

2.既然说到了Gson,那我们就把上面的字符串转成javaBean吧(说到bean,如果你闲麻烦,那么网上有自动生成的工具,也可以用as自带的生成)

public class TestBean {

 

    private AreaInfoBean areaInfo;

    private String code;

    private String msg;

    private List<PointListInfoBean> pointListInfo;

 

    public AreaInfoBean getAreaInfo() {

        return areaInfo;

    }

 

    public void setAreaInfo(AreaInfoBean areaInfo) {

        this.areaInfo = areaInfo;

    }

 

    public String getCode() {

        return code;

    }

 

    public void setCode(String code) {

        this.code = code;

    }

 

    public String getMsg() {

        return msg;

    }

 

    public void setMsg(String msg) {

        this.msg = msg;

    }

 

    public List<PointListInfoBean> getPointListInfo() {

        return pointListInfo;

    }

 

    public void setPointListInfo(List<PointListInfoBean> pointListInfo) {

        this.pointListInfo = pointListInfo;

    }

 

    public static class AreaInfoBean {

        private String AQI;

        private String AQILevel;

        private String areaName;

        private String highTemperature;

        private String lowTemperature;

        private String nowTemperature;

        private String weatherState;

        private String windDirect;

        private String windPower;

 

        public String getAQI() {

            return AQI;

        }

 

        public void setAQI(String AQI) {

            this.AQI = AQI;

        }

 

        public String getAQILevel() {

            return AQILevel;

        }

 

        public void setAQILevel(String AQILevel) {

            this.AQILevel = AQILevel;

        }

 

        public String getAreaName() {

            return areaName;

        }

 

        public void setAreaName(String areaName) {

            this.areaName = areaName;

        }

 

        public String getHighTemperature() {

            return highTemperature;

        }

 

        public void setHighTemperature(String highTemperature) {

            this.highTemperature = highTemperature;

        }

 

        public String getLowTemperature() {

            return lowTemperature;

        }

 

        public void setLowTemperature(String lowTemperature) {

            this.lowTemperature = lowTemperature;

        }

 

        public String getNowTemperature() {

            return nowTemperature;

        }

 

        public void setNowTemperature(String nowTemperature) {

            this.nowTemperature = nowTemperature;

        }

 

        public String getWeatherState() {

            return weatherState;

        }

 

        public void setWeatherState(String weatherState) {

            this.weatherState = weatherState;

        }

 

        public String getWindDirect() {

            return windDirect;

        }

 

        public void setWindDirect(String windDirect) {

            this.windDirect = windDirect;

        }

 

        public String getWindPower() {

            return windPower;

        }

 

        public void setWindPower(String windPower) {

            this.windPower = windPower;

        }

    }

 

    public static class PointListInfoBean {

        private int PM10;

        private int PM25;

        private double noise;

        private String pointId;

        private String pointName;

        private String recTime;

 

        public int getPM10() {

            return PM10;

        }

 

        public void setPM10(int PM10) {

            this.PM10 = PM10;

        }

 

        public int getPM25() {

            return PM25;

        }

 

        public void setPM25(int PM25) {

            this.PM25 = PM25;

        }

 

        public double getNoise() {

            return noise;

        }

 

        public void setNoise(double noise) {

            this.noise = noise;

        }

 

        public String getPointId() {

            return pointId;

        }

 

        public void setPointId(String pointId) {

            this.pointId = pointId;

        }

 

        public String getPointName() {

            return pointName;

        }

 

        public void setPointName(String pointName) {

            this.pointName = pointName;

        }

 

        public String getRecTime() {

            return recTime;

        }

 

        public void setRecTime(String recTime) {

            this.recTime = recTime;

        }

    }

}

 

3.这一步就开始用xutils请求网络了(请求网络无论你用那个都是差不多的)

       @Override

                     public void onSuccess(ResponseInfo<String> responseInfo) {

                           

                            String resultJson = responseInfo.result;

                            parseJson(resultJson);  

                     }

4.那开始用Gson解析从服务器返回的数据了

private void parseJson(String resultJson) {

                    

                     Gson gson = new Gson();

                     TestBean pointlistdata = gson.fromJson(resultJson, TestBean.class);//把数据填充到javaBean中(set进去get出来)

      

                     mAqi.setText(pointlistdata.getAreaInfo().getAQI());//以它为例,get出存进去的数据,以下都是

                     mColor.setBackgroundResource(GlobalConstents

                                   .getAqiStateDrawable(pointlistdata.getAreaInfo().getAQILevel()));

                     mWeather.setBackgroundResource(GlobalConstents.getWeathre(pointlistdata

                                   .getAreaInfo().getWeatherState()));

                     mLoc.setText(pointlistdata.getAreaInfo().getAreaName());

                     mText.setText(GlobalConstents.getAqiState(pointlistdata.getAreaInfo()

                                   .getAQILevel()));

                     mClimate.setText(pointlistdata.getAreaInfo().getNowTemperature() + "℃");// 当前温度

                     // 风向拼接

                     mWind2.setText(pointlistdata.getAreaInfo().getWindDirect()

                                   + pointlistdata.getAreaInfo().getWindPower());

                     mHumidity.setText(pointlistdata.getAreaInfo().gethumidity());

                     // 温度拼接

                     mTemperatrue.setText(pointlistdata.getAreaInfo().getLowTemperature()

                                   + "-" + pointlistdata.getAreaInfo().getHighTemperature() + "℃");

                                         

                     for (int i = 0; i < pointlistdata.getPointListInfo().size(); i++) {

                           

                            mTime.setText(DateUtils.getReciveHour(pointlistdata.getPointListInfo().get(i).getRecTime()));

                            pagers.add(new EquipmentPager(getActivity(),pointlistdata.getPointListInfo().get(i)));

                            Log.e("hhhh", "----"+pointlistdata.getPointListInfo().get(i).getPointId());

                     }

                    

                    

                     MyHomePagerAdapter adapter = new MyHomePagerAdapter(pointlistdata.getPointListInfo());

                     mviewPager.setAdapter(adapter);

       }

谢谢,希望对大家有用!

原文链接:http://www.apkbus.com/blog-784586-61889.html

0人推荐
随时随地看视频
慕课网APP