Android 網路請求json資料,解析json資料,產生對應的java bean類一步到位,快速開發

來源:互聯網
上載者:User

標籤:快速解析json 資料 volley 一

Android 網路請求一般都涉及到圖片和JSON資料,怎樣快速的請求網路JSON資料,解析JSON資料,並且一步產生自己想要的Java bean實體類?這個涉及到Android 開發效率的問題。由於接觸Android 網路這方面比較多,自然就找到一些好的方法來快速開發Android 網路模組的相關內容,接下來就為大家揭曉 一步快速請求,解析JSON 資料產生對應的Java bean實體類的方法。

註:我們先把思路講解下吧:

1.網路請求JSON資料代碼可以自己寫,當然我還是推薦使用網路上開源的穩定的架構---Volley,相信很多人應該瞭解這個開源架構吧,不知道的百度去,這是一個很好用的網路請求開源架構。

2.解析JSON 資料,最好的方法無疑是使用網路上線程的工具 jar包(Google的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資料是:

{    "weatherinfo": {        "city": "北京",        "cityid": "101010100",        "temp1": "5℃",        "temp2": "-3℃",        "weather": "晴",        "img1": "d0.gif",        "img2": "n0.gif",        "ptime": "11:00"    }}
那麼我們首先需要定義一個天氣資訊類:

import java.util.HashMap;import java.util.Map;import javax.annotation.Generated;import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.HashCodeBuilder;import org.apache.commons.lang.builder.ToStringBuilder;@Generated("org.jsonschema2pojo")public class Test {    private Weatherinfo weatherinfo;    private Map<String, Object> additionalProperties = new HashMap<String, Object>();    /**     *      * @return     *     The weatherinfo     */    public Weatherinfo getWeatherinfo() {        return weatherinfo;    }    /**     *      * @param weatherinfo     *     The weatherinfo     */    public void setWeatherinfo(Weatherinfo weatherinfo) {        this.weatherinfo = weatherinfo;    }    @Override    public String toString() {        return ToStringBuilder.reflectionToString(this);    }    public Map<String, Object> getAdditionalProperties() {        return this.additionalProperties;    }    public void setAdditionalProperty(String name, Object value) {        this.additionalProperties.put(name, value);    }    @Override    public int hashCode() {        return new HashCodeBuilder().append(weatherinfo).append(additionalProperties).toHashCode();    }    @Override    public boolean equals(Object other) {        if (other == this) {            return true;        }        if ((other instanceof Test) == false) {            return false;        }        Test rhs = ((Test) other);        return new EqualsBuilder().append(weatherinfo, rhs.weatherinfo).append(additionalProperties, rhs.additionalProperties).isEquals();    }}

import java.util.HashMap;import java.util.Map;import javax.annotation.Generated;import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.HashCodeBuilder;import org.apache.commons.lang.builder.ToStringBuilder;@Generated("org.jsonschema2pojo")public class Weatherinfo {    private String city;    private String cityid;    private String temp1;    private String temp2;    private String weather;    private String img1;    private String img2;    private String ptime;    private Map<String, Object> additionalProperties = new HashMap<String, Object>();    /**     *      * @return     *     The city     */    public String getCity() {        return city;    }    /**     *      * @param city     *     The city     */    public void setCity(String city) {        this.city = city;    }    /**     *      * @return     *     The cityid     */    public String getCityid() {        return cityid;    }    /**     *      * @param cityid     *     The cityid     */    public void setCityid(String cityid) {        this.cityid = cityid;    }    /**     *      * @return     *     The temp1     */    public String getTemp1() {        return temp1;    }    /**     *      * @param temp1     *     The temp1     */    public void setTemp1(String temp1) {        this.temp1 = temp1;    }    /**     *      * @return     *     The temp2     */    public String getTemp2() {        return temp2;    }    /**     *      * @param temp2     *     The temp2     */    public void setTemp2(String temp2) {        this.temp2 = temp2;    }    /**     *      * @return     *     The weather     */    public String getWeather() {        return weather;    }    /**     *      * @param weather     *     The weather     */    public void setWeather(String weather) {        this.weather = weather;    }    /**     *      * @return     *     The img1     */    public String getImg1() {        return img1;    }    /**     *      * @param img1     *     The img1     */    public void setImg1(String img1) {        this.img1 = img1;    }    /**     *      * @return     *     The img2     */    public String getImg2() {        return img2;    }    /**     *      * @param img2     *     The img2     */    public void setImg2(String img2) {        this.img2 = img2;    }    /**     *      * @return     *     The ptime     */    public String getPtime() {        return ptime;    }    /**     *      * @param ptime     *     The ptime     */    public void setPtime(String ptime) {        this.ptime = ptime;    }    @Override    public String toString() {        return ToStringBuilder.reflectionToString(this);    }    public Map<String, Object> getAdditionalProperties() {        return this.additionalProperties;    }    public void setAdditionalProperty(String name, Object value) {        this.additionalProperties.put(name, value);    }    @Override    public int hashCode() {        return new HashCodeBuilder().append(city).append(cityid).append(temp1).append(temp2).append(weather).append(img1).append(img2).append(ptime).append(additionalProperties).toHashCode();    }    @Override    public boolean equals(Object other) {        if (other == this) {            return true;        }        if ((other instanceof Weatherinfo) == false) {            return false;        }        Weatherinfo rhs = ((Weatherinfo) other);        return new 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();    }}

上面的這些代碼你們不會一個一個去敲吧???萬一 類的哪個成員變數敲錯了,跟Json資料裡面的不一樣?那麼 FastJson 就解析出錯了啊!!!!!,那麼怎麼才不許要自己敲這些代碼呢? 這裡教大家一個辦法:請看部落格:Android Json 使用jsonschema2pojo產生.java檔案檔案。

二.網路請求Json 資料,大家知道,在Android 中寫一個簡單的網路請求任務都需要寫 很長一段代碼,並且還需要注意android 網路請求必須在子線程中處理,所以跟新UI就得注意了。這裡我們使用2013年Google大會上提供的開源架構 Volley ,使用這個架構請求網路非常方便,不瞭解的請看部落格:Android Volley完全解析(一),初識Volley的基本用法 

因為我們這一節重點是一步解析JSON資料獲得 Java bean,所以我自己仿照 Volley 的StringRequest 重新自訂了一個FastJosnRequest 類來使用,使用這個類可以直接獲得Java bean實體類,都不需要你自己解析JSON資料了,給你省了很多事情。

FastJson jar包下載連結:http://download.csdn.net/detail/feidu804677682/8341467

FastJosnRequest類的實現如下:

/* * 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. */package com.android.volley.toolbox;import com.alibaba.fastjson.JSON;import com.android.volley.AuthFailureError;import com.android.volley.NetworkResponse;import com.android.volley.ParseError;import com.android.volley.Request;import com.android.volley.Response;import com.android.volley.Response.ErrorListener;import com.android.volley.Response.Listener;import java.io.UnsupportedEncodingException;import java.util.Collections;import java.util.Map;/** * A canned request for retrieving the response body at a given URL as a String. *  * @param <T> */public class FastJsonRequest<T> extends Request<T> {private final Listener<T> mListener;private final Map<String, String> mParams;private Map<String, String> mHeaders;private Class<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 */public FastJsonRequest(int method, 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 */public FastJsonRequest(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);}@Overrideprotected Map<String, String> getParams() throws AuthFailureError {return mParams;}@Overridepublic Map<String, String> getHeaders() throws AuthFailureError {if (null == mHeaders) {mHeaders = Collections.emptyMap();}return mHeaders;}@Overrideprotected void deliverResponse(T response) {mListener.onResponse(response);}@Overrideprotected Response<T> parseNetworkResponse(NetworkResponse response) {try {String jsonString = new String(response.data,HttpHeaderParser.parseCharset(response.headers));return Response.success(JSON.parseObject(jsonString, mClass),HttpHeaderParser.parseCacheHeaders(response));} catch (UnsupportedEncodingException e) {return Response.error(new ParseError(e));}}}
這裡我直接使用了FastJson jar包來協助完成解析JSON資料,直接返回給使用者實體類,而不許要使用者去解析JSON資料。研究過Volley 開源架構的人會發現,我這個類中重寫了幾個方法,getParams()和getHeaders()兩個方法。因為我需要相容來自使用者不同方式的請求(GET和POST)。當使用GET請求的時候 params 和headers 傳值 null 進來即可。

三. 介面的使用如下:

package com.example.fastjson;import java.util.List;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.android.volley.RequestQueue;import com.android.volley.Response.ErrorListener;import com.android.volley.Response.Listener;import com.android.volley.VolleyError;import com.android.volley.VolleyLog;import com.android.volley.toolbox.FastJsonRequest;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import com.example.fastjson.bean.Apk;import com.example.fastjson.bean.App;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;public class MainActivity extends Activity {String url = "http://www.weather.com.cn/data/cityinfo/101010100.html";RequestQueue mQueue;String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mQueue = Volley.newRequestQueue(this);mQueue = Volley.newRequestQueue(this);FastJsonRequest<Test> fRequest = new FastJsonRequest<Test>(url, null,null, Test.class, new Listener<Test>() {@Overridepublic void onResponse(Test response) {Log.i(TAG, response.getWeatherinfo().toString());}}, new ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {}});mQueue.add(fRequest);}}

結果列印如下:

<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分鐘就搞定。



Android 網路請求json資料,解析json資料,產生對應的java bean類一步到位,快速開發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.