Android : How to convert a json response from volley that is too big to be done in the UI thread?

on Wednesday, April 15, 2015


I have been looking into volley and basically all the example are similar to the following from the official doc:



JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
mTxtDisplay.setText("Response: " + response.toString());
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub

}
});


So my understanding is that we do a request in a background thread and Volley takes care of these details and we get the response in the UI thread when the onResponse method is called i.e.



@Override
public void onResponse(JSONObject response) {
mTxtDisplay.setText("Response: " + response.toString());
}


My question is what happens if the JSONObject response from the server is non-trivial, actually quite big and to update the UI we need to map to a e.g. GSON class to get the fields we need?

I assume doing the conversion inside the onResponse is the wrong thing since the conversion could be time consuming and we will be doing that in the UI thread.

I guess I could fire an new thread at that point but getting the results from that new thread to update the UI in the UI thread can get messy.

I believe this should be a standard problem so there must be a standard clean solution/approach.

What is the clean/standard approach for this conversion?


0 comments:

Post a Comment