Android 网络请求json数据,解析json数据,生成对应的java bean类一步到位,快速开发

来源:互联网 发布:广场舞短袖上衣淘宝网 编辑:程序博客网 时间:2024/05/02 04:50
[转]红黑联盟  作者 feidu    http://www.2cto.com/kf/201501/368011.html

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

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

1.网络请求JSON数据代码可以自己写,当然我还是推荐使用网络上开源的稳定的框架---Volley,相信很多人应该了解这个开源框架吧,不知道的百度去,这是一个很好用的网络请求开源框架。

2.解析JSON 数据,最好的方法无疑是使用网络上线程的工具 jar包(谷歌的GSON 阿里的FastJson),我这里选择的是阿里的FastJson,FastJson有优势,具体优势后面讲解。

3.解析JSON数据后要将数据保存到 实体类中,我们需要自己定义实体类,但是,在使用FastJson 解析JSON数据的时候必须确保 JSON 数据字段和 实体类的成员变量名字相同,否则FastJson 是解析不出来的(Gson也解析不出来),但是使用FastJson 不区分实体类成员变量的大小写,而Gson 区分,这就是为什么我选择FastJson解析JSON数据了。


一.我们需要解析JSON数据必然需要先定义 JSON数据信息实体类,然后才能解析JSON数据,将数据保存到类中。但是,定义这个类不需要一个变量的去敲代码,而且有可能出错。这里我们拿中国天气预报的一条JSON数据说明,http://www.weather.com.cn/data/cityinfo/101010100.html ,浏览器请求后获得的JSON数据是:

?
1
2
3
4
5
6
7
8
9
10
11
12
{
    "weatherinfo": {
        "city":"北京",
        "cityid":"101010100",
        "temp1":"5℃",
        "temp2":"-3℃",
        "weather":"晴",
        "img1":"d0.gif",
        "img2":"n0.gif",
        "ptime":"11:00"
    }
}
那么我们首先需要定义一个天气信息类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
importjava.util.HashMap;
importjava.util.Map;
importjavax.annotation.Generated;
importorg.apache.commons.lang.builder.EqualsBuilder;
importorg.apache.commons.lang.builder.HashCodeBuilder;
importorg.apache.commons.lang.builder.ToStringBuilder;
 
@Generated("org.jsonschema2pojo")
publicclass Test {
 
    privateWeatherinfo weatherinfo;
    privateMap<string, object=""> additionalProperties = newHashMap<string, object="">();
 
    /**
     *
     * @return
     *     The weatherinfo
     */
    publicWeatherinfo getWeatherinfo() {
        returnweatherinfo;
    }
 
    /**
     *
     * @param weatherinfo
     *     The weatherinfo
     */
    publicvoid setWeatherinfo(Weatherinfo weatherinfo) {
        this.weatherinfo = weatherinfo;
    }
 
    @Override
    publicString toString() {
        returnToStringBuilder.reflectionToString(this);
    }
 
    publicMap<string, object=""> getAdditionalProperties() {
        returnthis.additionalProperties;
    }
 
    publicvoid setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }
 
    @Override
    publicint hashCode() {
        returnnew HashCodeBuilder().append(weatherinfo).append(additionalProperties).toHashCode();
    }
 
    @Override
    publicboolean equals(Object other) {
        if(other == this) {
            returntrue;
        }
        if((other instanceofTest) == false) {
            returnfalse;
        }
        Test rhs = ((Test) other);
        returnnew EqualsBuilder().append(weatherinfo, rhs.weatherinfo).append(additionalProperties, rhs.additionalProperties).isEquals();
    }
 
}</string,></string,></string,>

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
importjava.util.HashMap;
importjava.util.Map;
importjavax.annotation.Generated;
importorg.apache.commons.lang.builder.EqualsBuilder;
importorg.apache.commons.lang.builder.HashCodeBuilder;
importorg.apache.commons.lang.builder.ToStringBuilder;
 
@Generated("org.jsonschema2pojo")
publicclass Weatherinfo {
 
    privateString city;
    privateString cityid;
    privateString temp1;
    privateString temp2;
    privateString weather;
    privateString img1;
    privateString img2;
    privateString ptime;
    privateMap<string, object=""> additionalProperties = newHashMap<string, object="">();
 
    /**
     *
     * @return
     *     The city
     */
    publicString getCity() {
        returncity;
    }
 
    /**
     *
     * @param city
     *     The city
     */
    publicvoid setCity(String city) {
        this.city = city;
    }
 
    /**
     *
     * @return
     *     The cityid
     */
    publicString getCityid() {
        returncityid;
    }
 
    /**
     *
     * @param cityid
     *     The cityid
     */
    publicvoid setCityid(String cityid) {
        this.cityid = cityid;
    }
 
    /**
     *
     * @return
     *     The temp1
     */
    publicString getTemp1() {
        returntemp1;
    }
 
    /**
     *
     * @param temp1
     *     The temp1
     */
    publicvoid setTemp1(String temp1) {
        this.temp1 = temp1;
    }
 
    /**
     *
     * @return
     *     The temp2
     */
    publicString getTemp2() {
        returntemp2;
    }
 
    /**
     *
     * @param temp2
     *     The temp2
     */
    publicvoid setTemp2(String temp2) {
        this.temp2 = temp2;
    }
 
    /**
     *
     * @return
     *     The weather
     */
    publicString getWeather() {
        returnweather;
    }
 
    /**
     *
     * @param weather
     *     The weather
     */
    publicvoid setWeather(String weather) {
        this.weather = weather;
    }
 
    /**
     *
     * @return
     *     The img1
     */
    publicString getImg1() {
        returnimg1;
    }
 
    /**
     *
     * @param img1
     *     The img1
     */
    publicvoid setImg1(String img1) {
        this.img1 = img1;
    }
 
    /**
     *
     * @return
     *     The img2
     */
    publicString getImg2() {
        returnimg2;
    }
 
    /**
     *
     * @param img2
     *     The img2
     */
    publicvoid setImg2(String img2) {
        this.img2 = img2;
    }
 
    /**
     *
     * @return
     *     The ptime
     */
    publicString getPtime() {
        returnptime;
    }
 
    /**
     *
     * @param ptime
     *     The ptime
     */
    publicvoid setPtime(String ptime) {
        this.ptime = ptime;
    }
 
    @Override
    publicString toString() {
        returnToStringBuilder.reflectionToString(this);
    }
 
    publicMap<string, object=""> getAdditionalProperties() {
        returnthis.additionalProperties;
    }
 
    publicvoid setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }
 
    @Override
    publicint hashCode() {
        returnnew HashCodeBuilder().append(city).append(cityid).append(temp1).append(temp2).append(weather).append(img1).append(img2).append(ptime).append(additionalProperties).toHashCode();
    }
 
    @Override
    publicboolean equals(Object other) {
        if(other == this) {
            returntrue;
        }
        if((other instanceofWeatherinfo) == false) {
            returnfalse;
        }
        Weatherinfo rhs = ((Weatherinfo) other);
        returnnew EqualsBuilder().append(city, rhs.city).append(cityid, rhs.cityid).append(temp1, rhs.temp1).append(temp2, rhs.temp2).append(weather, rhs.weather).append(img1, rhs.img1).append(img2, rhs.img2).append(ptime, rhs.ptime).append(additionalProperties, rhs.additionalProperties).isEquals();
    }
 
}</string,></string,></string,>

上面的这些代码你们不会一个一个去敲吧???万一 类的哪个成员变量敲错了,跟Json数据里面的不一样?那么 FastJson 就解析出错了啊!!!!!,那么怎么才不许要自己敲这些代码呢? 这里教大家一个办法:请看博客:Android Json 使用jsonschema2pojo生成.java文件文件 。

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

因为我们这一节重点是一步解析JSON数据获得 Java bean,所以我自己仿照 Volley 的StringRequest 重新自定义了一个FastJosnRequest 类来使用,使用这个类可以直接获得Java bean实体类,都不需要你自己解析JSON数据了,给你省了很多事情。

FastJson jar包下载链接:http://download.csdn.net/detail/feidu804677682/8341467

FastJosnRequest类的实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (C) 2011 The Android Open Source Project Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
 
packagecom.android.volley.toolbox;
 
importcom.alibaba.fastjson.JSON;
importcom.android.volley.AuthFailureError;
importcom.android.volley.NetworkResponse;
importcom.android.volley.ParseError;
importcom.android.volley.Request;
importcom.android.volley.Response;
importcom.android.volley.Response.ErrorListener;
importcom.android.volley.Response.Listener;
importjava.io.UnsupportedEncodingException;
importjava.util.Collections;
importjava.util.Map;
 
/**
 * A canned request for retrieving the response body at a given URL as a String.
 *
 * @param <t>
 */
publicclass FastJsonRequest<t> extendsRequest<t> {
    privatefinal Listener<t> mListener;
    privatefinal Map<string, string=""> mParams;
    privateMap<string, string=""> mHeaders;
    privateClass<t> mClass;
 
    /**
     * Creates a new request with the given method.
     *
     * @param method
     *            the request {@link Method} to use
     * @param url
     *            URL to fetch the string at
     * @param params
     *            Params for the POST request.
     * @param headers
     *            Headers for the POST request.
     * @param listener
     *            Listener to receive the String response
     * @param errorListener
     *            Error listener, or null to ignore errors
     */
    publicFastJsonRequest(intmethod, String url, Map<string, string=""> params,
            Map<string, string=""> headers, Class<t> mClass, Listener<t> listener,
            ErrorListener errorListener) {
        super(method, url, errorListener);
        mListener = listener;
        mParams = params;
        mHeaders = headers;
        this.mClass = mClass;
    }
 
    /**
     * Creates a new GET or POST request, if request params is null the request
     * is GET otherwise POST request.
     *
     * @param url
     *            URL to fetch the string at
     * @param params
     *            Params for the POST request.
     * @param headers
     *            Headers for the POST request.
     * @param listener
     *            Listener to receive the String response
     * @param errorListener
     *            Error listener, or null to ignore errors
     */
    publicFastJsonRequest(String url, Map<string, string=""> params,
            Map<string, string=""> headers, Class<t> mClass, Listener<t> listener,
            ErrorListener errorListener) {
        this(null== params ? Method.GET : Method.POST, url, params, headers,
                mClass, listener, errorListener);
    }
 
    @Override
    protectedMap<string, string=""> getParams() throwsAuthFailureError {
        returnmParams;
    }
 
    @Override
    publicMap<string, string=""> getHeaders() throwsAuthFailureError {
        if(null== mHeaders) {
            mHeaders = Collections.emptyMap();
        }
        returnmHeaders;
    }
 
    @Override
    protectedvoid deliverResponse(T response) {
        mListener.onResponse(response);
    }
 
    @Override
    protectedResponse<t> parseNetworkResponse(NetworkResponse response) {
        try{
            String jsonString = newString(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            returnResponse.success(JSON.parseObject(jsonString, mClass),
                    HttpHeaderParser.parseCacheHeaders(response));
        }catch(UnsupportedEncodingException e) {
            returnResponse.error(newParseError(e));
        }
    }
}
</t></string,></string,></t></t></string,></string,></t></t></string,></string,></t></string,></string,></t></t></t></t>
这里我直接使用了FastJson jar包来帮助完成解析JSON数据,直接返回给用户实体类,而不许要用户去解析JSON数据。研究过Volley 开源框架的人会发现,我这个类中重写了几个方法,getParams()和getHeaders()两个方法。因为我需要兼容来自用户不同方式的请求(GET和POST)。当使用GET请求的时候 params 和headers 传值 null 进来即可。

三. 接口的使用如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
packagecom.example.fastjson;
 
importjava.util.List;
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.JSONObject;
importcom.android.volley.RequestQueue;
importcom.android.volley.Response.ErrorListener;
importcom.android.volley.Response.Listener;
importcom.android.volley.VolleyError;
importcom.android.volley.VolleyLog;
importcom.android.volley.toolbox.FastJsonRequest;
importcom.android.volley.toolbox.StringRequest;
importcom.android.volley.toolbox.Volley;
importcom.example.fastjson.bean.Apk;
importcom.example.fastjson.bean.App;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.util.Log;
importandroid.view.Menu;
 
publicclass MainActivity extendsActivity {
 
    String url = "http://www.weather.com.cn/data/cityinfo/101010100.html";
    RequestQueue mQueue;
    String TAG = "MainActivity";
 
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mQueue = Volley.newRequestQueue(this);
 
        mQueue = Volley.newRequestQueue(this);
        FastJsonRequest<test> fRequest = newFastJsonRequest<test>(url, null,
                null, Test.class,newListener<test>() {
 
                    @Override
                    publicvoid onResponse(Test response) {
                        Log.i(TAG, response.getWeatherinfo().toString());
                    }
                },newErrorListener() {
 
                    @Override
                    publicvoid onErrorResponse(VolleyError error) {
 
                    }
                });
        mQueue.add(fRequest);
    }
 
}
</test></test></test>

结果打印如下:

?
1
<span style="color:#009900;">Weatherinfo [city=北京, cityId=101010100, temp1=5℃, temp2=-3℃, weather=晴, img1=d0.gif, img2=n0.gif, ptime=11:00]</span>

OK,完成。源代码就不附了,主要的是思路。自己写代码,可以学到很多东西。如果你学会了这种方法,那么解析json数据本来要一个上午敲代码的现在只需要10分钟就搞定。
0 0