Custom fastjsonrequest
we're using the volley .
It's pretty good.
I kind of like using volley .
Very convenient
There's a lot of Request in Volley .
Like the things we used to do.
Stringrequest
Jsonobjectrequest
Jsonarrayrequest
Imagerequest
after we used the jsonobjectrequest request ,
Get jsonobject
Of course we have to parse
We usually parse a lot of third-party libraries
Like Gson,fastjson and so on .
We're going to use Fastjson now .
But we're not slowly response
Instead of customizing a fastjsonrequest
In this custom request, the direct parsing ,
After the request succeeds, it gets the parsed object
Put the Fastjson-1.2.4.jar in the Libs first.
Add as Library
This Fastjson was developed by Ali and was made in China ,
It is said that GitHub 's top -domestic first- name is almost Ali's .
Come on , code code .
let's get a class that inherits volley 's Request .
public class Fastjsonrequest extends request{
}
implement a two-way
@Override
Protected Response parsenetworkresponse (Networkresponse Response) {
return null;
}
@Override
protected void Deliverresponse (Object response) {
}
Here the system may be prompted to implement a CompareTo method without
and then come up with two more methods of construction
Public fastjsonrequest (String URL, Response.errorlistener listener) {
Super (URL, listener);
}
Public fastjsonrequest (int method, String URL, Response.errorlistener listener) {
Super (method, URL, listener);
}
and then we're just imitating volley 's stringrequest to write this fastjsonrequest .
Two ways to implement
1.parseNetworkResponse
2.deliverResponse
let's go inside the stringrequest and see how parsenetworkresponse this is.
@Override
Protected response<string> parsenetworkresponse (Networkresponse Response) {
String parsed;
try {
parsed = new String (Response.data, Httpheaderparser.parsecharset (response.headers));
} catch (Unsupportedencodingexception e) {
parsed = new String (response.data);
}
Return response.success (parsed, httpheaderparser.parsecacheheaders (Response));
}
Let's take a look at this piece of code.
First , the return type is a response<string>
The parameters of the method are (networkresponse response),
Then a String parsed is defined;
Trycatch capturing unsupportedencodingexception not supported encoding exceptions
No mistakes ,
Parsed=new String (Response.data, Httpheaderparser.parsecharset (response.headers));
Something went wrong ,
parsed = new String (response.data);
Last return
Return response.success (parsed, httpheaderparser.parsecacheheaders (Response));
now look at the smattering
But we can see that this String parsed
Is the string we need to parse.
Let's just copy this piece of code and put it in our parsenetworkresponse method .
Just now , we're going to put the parsing process in our fastjsonrequest custom Request .
So we're going to put the parsing process in this way and execute it.
we use the JSON parse method to parse
Json.parseobject (parsed,???);
The second parameter would have been the Bean class where we stored the parse result.
But now we have this fastjsonrequest custom request is generic
So we don't know what Bean class to use to receive
So we're going to use a dynamic byte-code object to represent
And we're going to use generics to dynamically control
public class Fastjsonrequest<t> extends request<t>{...}
Private class<t> Mclazz;
@Override
Protected Response parsenetworkresponse (Networkresponse Response) {
String parsed;
try {
parsed = new String (Response.data, Httpheaderparser.parsecharset (response.headers));
} catch (Unsupportedencodingexception e) {
parsed = new String (response.data);
}
T t= Json.parse Object (parsed, mclazz);
return response.success (t, Httpheaderparser.parsecacheheaders (Response));
}
When we return, we're going to pass the T - object out .
and then the deliverresponse method .
we're going to look inside the stringrequest first .
Inside is
Private final listener<string> Mlistener;
@Override
protected void Deliverresponse (String response) {
Mlistener.onresponse (response);
}
we're the same copy , But our generics are T .
So generics are changed to T -
Private final listener<t> Mlistener;
@Override
protected void Deliverresponse (T response) {
Mlistener.onresponse (response);
}
and then we look at the construction method
Two construction methods are only Response.errorlistener,
We usually have response.listener When we use them , so we put Listener in .
Public fastjsonrequest (String URL, response.listener<t> successlistener, Response.errorlistener Listener) {
Super (URL, listener);
Mlistener=successlistener;
}
Public fastjsonrequest (int method, String URL, response.listener<t> successlistener, Response.errorlistener listener) {
Super (method, URL, listener);
Mlistener=successlistener;
}
and our class<t> clazz is going to pass it in .
Public fastjsonrequest (String URL, response.listener<t> successlistener, class<t> clazz, Response.errorlistener listener) {
Super (URL, listener);
Mlistener = Successlistener;
Mclazz=clazz;
}
Public fastjsonrequest (int method, String URL, response.listener<t> successlistener,class<t> clazz , Response.errorlistener Listener) {
Super (method, URL, listener);
Mlistener = Successlistener;
Mclazz=clazz;
}
simply Comb
In fact, our process is very simple , almost identical to Stringrequest.
It's just that we have 2 differences .
The first is that we joined the parsing process
The second is that we have changed <String> generics into our own dynamic generics <T>
And we have to add the parameters.
1 x Listener and 1 Class
One is what is needed , a callback after a successful request
One is the dynamic bytecode object that we need
let's use it.
String url= "Http://000.000.000.000/file/product.json";
fastjsonrequest<productbean> productfastjsonrequest = new fastjsonrequest<productbean> (URL, new Response.listener<productbean> () {
@Override
public void Onresponse (Productbean response) {
Mtv.settext (Response.data.get (0). Name);
}
}, Productbean.class, new Response.errorlistener () {
@Override
public void Onerrorresponse (Volleyerror error) {
Mtv.settext ("fail");
}
});
Requestqueue requestqueue = Volley.newrequestqueue (GetContext ());
Requestqueue.add (productfastjsonrequest);
194_ Custom Fastjsonrequest