There is a protected response<result<t>> parsenetworkresponse (Networkresponse Response) {} function in the volley framework. The JSON string obtained from the server or in the cache is parsed in this function.
New String (Response.data, Httpheaderparser.parsecharset (Response.headers, HTTP. utf_8) result<T> result = Gsonutil.getresult (jsonstring, Mtypetoken);
We can define a Gsonutil class. It writes some functional functions that Gson and JSON convert to each other. For example, the GetResult function is used. Use this function to get the result<t> of this javabean. That is, the specification returned, which is negotiated by front-end developers and background developers. Now that I get the JavaBean, then I'm going to get the data in the JSON as simple as getting a data from a class.
public static <T> T getresult (String jsondata, Typetoken<t> type) {Gson Gson = new Gsonbuilder (). Excludefieldswithoutexposeannotation (). Create (); return (T) Gson.fromjson (Jsondata, Type.GetType ()); }
Result<t> class: For example: (the JSON string returned in the background such as: {"err": "Success", "errno": 1, "rsm{" UserId ":" 1 "," email ":"? "," Password ":"? "," FirstName ":" hu "," LastName ":" ann "," Gender ":" 1 "," Photo ":"/picture/userphoto/ 1422624411423.PNG "," Autograph ":" "," Introduce ":" "," Createtime ":" 4 days ago "}} failed to return {" Err ":" user does not exist ! ", errno": 0} )
PackageWhu.cn.whushare.bean;ImportCom.google.gson.annotations.Expose;ImportCom.google.gson.annotations.SerializedName; Public classResult<t>{@Expose @SerializedName ("CurrentPage") Private Longcurrentpage; @Expose @SerializedName ("PageSize") Private LongpageSize; @Expose @SerializedName ("Alldatacount") Private LongAlldatacount; @Expose @SerializedName ("NextPage") Private LongNextPage; @Expose @SerializedName ("Errno") Private intCode; @Expose @SerializedName ("Err") PrivateString msg; Private intTotal ; @Expose @SerializedName ("RSM") PrivateT content; Publicresult (T result) { This. Content =result; } PublicResult (intcode, T result) { This. Code =Code; This. Content =result; } Public LongGetcurrentpage () {returncurrentpage; } Public voidSetcurrentpage (intcurrentpage) { This. currentpage =currentpage; } Public Longgetpagesize () {returnpageSize; } Public voidSetPageSize (intpageSize) { This. pageSize =pageSize; } Public LongGetalldatacount () {returnAlldatacount; } Public voidSetalldatacount (intAlldatacount) { This. Alldatacount =Alldatacount; } Public LongGetnextpage () {returnNextPage; } Public voidSetnextpage (intnextPage) { This. NextPage =NextPage; } Public intGetCode () {returnCode; } Public voidSetcode (intcode) { This. Code =Code; } PublicString getmsg () {returnmsg; } Public voidsetmsg (String msg) { This. msg =msg; } Public intgettotal () {returnTotal ; } Public voidSettotal (intTotal ) { This. Total =Total ; } PublicT getcontent () {returncontent; } Public voidsetcontent (T content) { This. Content =content; }}
Android parsing JSON strings with Gson