[HTTP] JSON data, http json data
With the development of Android and the contributions of various great gods, we have more and more available wheels. For example, the HTTP request framework includes its own Volley, Square okhttp, async-http-lib, and the aggregate version of xUtils and AFinal. I think you must have used one of them.
Of course, Stay does not come to science today, but to think about a problem with everyone. For the moment, we will not mention how much Optimization they have made internally, so we will talk about the data returned by lib.
In the return values of common http requests, files and JSON are the vast majority (images have other frameworks, which are not considered here ). All file downloads have special response, which will help you download to the specified path. This is certainly supported. What about JSON? It seems that a JSONObject or JSONArray is returned.
I am going to do everything well. What is the ghost of returning JSONObject? Do I have to write the parsing and deserialization into my own desired object? That is what programmers do at the lowest level. Fortunately, we are not stupid. We also have GSON, fastJson, and Jackson to help us complete this step.
For example, the data returned by the server: (double quotation marks are not added, occupying the location, Do not spray)
{name:stay, age:17, job:soho}
Corresponding object:
Class User{ public String name, public int age, public String job}
Well, we only need to get the result in the response callback and call json-lib deserialization. For example:
User user = gson.fromJson(result, User.class)
Now we can use the user object to update the UI, right. There is only one line of code, and no obsessive-compulsive disorder can be tolerated.
Next, let's look at the following json data:
{resCode:200, data:{name:Stay, age:17, job:soho}, msg:success}{resCode:401, data:{}, msg:token invalid}
I'm going. What is this? I don't have to comply with the http protocol. I returned a uniform 200 error code. Isn't the token legal .. Let alone, many companies define the data returned in this way.
So what should we do .. Write more for parsing.
JSONObject json = new JSONObject(result)JSONObject data = json.optJSONObject("data")if(data != null){ User user = gson.fromJson(data.toString(), User.class)}
Oh, my God, even if you are not obsessive, you may not be able to write so much code for every API request.
So many BB attacks, do you know what you want to express?
Why not directly convert json into the Object User we want and then call back?
In addition, when the json data is large, deserialization is still time-consuming. Is it possible that the UI will be stuck.
Is this possible? Of course you can. Otherwise, Stay will pave the way for so much. But before Stay says the solution, you can try to implement it on your own.
The String format is JSON.
Every time we get the JSON String, we perform a step of deserializing the object.
Gson. fromJson requires two parameters (String JSON, Class dest)
The callback parameter must be changed to onResponse (User user)
The framework layer must know the Class dest.
If you can think of these things clearly, you can easily extend those open-source frameworks, and you will not need to write json parsing in the future.
Let's talk about this. Let's leave some time for everyone to think about it. Let's talk about the solution below.