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

Android Volley请求json数据,fastJson解析json数据

2015-12-15 14:58 471 查看
参考:/article/1480917.html

关于JSON的官方介绍

fastJson 下载地址:

作者的git:http://git.oschina.net/wenshao/fastjson

安卓版 jar下载:http://download.csdn.net/detail/xx326664162/9357657

volley下载地址:

官方源码地址:git clone https://android.googlesource.com/platform/frameworks/volley

安卓 jar下载:http://download.csdn.net/detail/xx326664162/9357683

Android 网络请求一般都涉及到图片和JSON数据,怎样快速的请求网络JSON数据,解析JSON数据,并且一步生成自己想要的Java bean实体类?这个涉及到Android 开发效率的问题。由于接触Android 网络这方面比较多,自然就找到一些好的方法来快速开发Android 网络模块的相关内容,接下来就为大家揭晓 一步快速请求,解析JSON 数据生成对应的Java bean实体类的方法。

注:我们先把思路讲解下吧:

1、 网络请求JSON数据代码可以自己写,当然我还是推荐使用网络上开源的稳定的框架—Volley很好用的网络请求开源框架。

2、 解析JSON 数据,最好的方法无疑是使用网络上线程的工具 jar包(谷歌的GSON 阿里的FastJson),选择的是阿里的FastJson

阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:

速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser。包括自称最快的JackJson;

功能强大,完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自省;无依赖,能够直接运行在Java SE 5.0以上版本;支持Android;开源 (Apache 2.0)

3、解析JSON数据后要将数据保存到 java Bean实体类中,我们需要自己定义实体类,

使用FastJson 解析JSON数据的时候必须确保 JSON 数据字段和 java Bean实体类的成员变量名字相同,否则FastJson 是解析不出来的(Gson也解析不出来)

使用FastJson 不区分实体类成员变量的大小写,而Gson 区分

示例:

一、我们需要解析JSON数据,需要先定义对应的Java Bean实体类,然后才能解析JSON数据,将数据保存到类中

浏览器请求http://www.weather.com.cn/data/cityinfo/101010100.html后获得的JSON数据是:

[code]{  
    "weatherinfo": {  
        "city": "北京",  
        "cityid": "101010100",  
        "temp1": "5℃",  
        "temp2": "-3℃",  
        "weather": "晴",  
        "img1": "d0.gif",  
        "img2": "n0.gif",  
        "ptime": "11:00"  
    }  
}


定义一个天气信息类,可以手动或者使用Android studio 插件GsonFormat

[code]public class Weatherinfo {

    /**
     * city : 北京
     * cityid : 101010100
     * temp1 : 15℃
     * temp2 : 5℃
     * weather : 多云
     * img1 : d1.gif
     * img2 : n1.gif
     * ptime : 08:00
     */

    private WeatherinfoEntity weatherinfo;

    public void setWeatherinfo(WeatherinfoEntity weatherinfo) {
        this.weatherinfo = weatherinfo;
    }

    public WeatherinfoEntity getWeatherinfo() {
        return weatherinfo;
    }

    public static class WeatherinfoEntity {
        private String city;
        private String cityid;
        private String temp1;
        private String temp2;
        private String weather;
        private String img1;
        private String img2;
        private String ptime;

        public void setCity(String city) {
            this.city = city;
        }

        public void setCityid(String cityid) {
            this.cityid = cityid;
        }

        public void setTemp1(String temp1) {
            this.temp1 = temp1;
        }

        public void setTemp2(String temp2) {
            this.temp2 = temp2;
        }

        public void setWeather(String weather) {
            this.weather = weather;
        }

        public void setImg1(String img1) {
            this.img1 = img1;
        }

        public void setImg2(String img2) {
            this.img2 = img2;
        }

        public void setPtime(String ptime) {
            this.ptime = ptime;
        }

        public String getCity() {
            return city;
        }

        public String getCityid() {
            return cityid;
        }

        public String getTemp1() {
            return temp1;
        }

        public String getTemp2() {
            return temp2;
        }

        public String getWeather() {
            return weather;
        }

        public String getImg1() {
            return img1;
        }

        public String getImg2() {
            return img2;
        }

        public String getPtime() {
            return ptime;
        }
    }
}


二、网络请求Json 数据,大家知道,在Android 中写一个简单的网络请求任务都需要写 很长一段代码,并且还需要注意android 网络请求必须在子线程中处理,所以跟新UI就得注意了。这里我们使用2013年谷歌大会上提供的开源框架 Volley ,使用这个框架请求网络非常方便

[code]       RequestQueue mQueue;

        mQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://www.weather.com.cn/adat/cityinfo/101010100.html", null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("TAG", response.toString());
                        String mJSON = response.toString();
                        Weatherinfo weatherinfo = new Weatherinfo();
                        weatherinfo = JSON.parseObject(mJSON, Weatherinfo.class);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("TAG", error.getMessage(), error);
                    }
                });
        mQueue.add(jsonObjectRequest);


三、上述代码的第11 行,就是使用fastJson将JSON数据解析成java Bean 的实体类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: