Parse Android Volley JSONArray answer

I am sending a JSONArray GET request with Volley and it will return the specified JSON array. Here is my request:

JsonArrayRequest getRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { Log.d("Response", response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("Error.Response", error.toString()); } } ); VolleySingleton.getInstance(this).addToRequestQueue(getRequest); //Call to get dashboard feed } 

As you can see, I'm just leaving the answer now. I want to parse an array and show the results as a list. The documentation for this is small and I'm pretty green in terms of Android dev. What is the correct way to parse a Volley JSON array and display the results as a list? I gathered that I should use parseNetworkResponse , but not sure how to implement it.

+7
java json android android-volley
source share
3 answers

I would recommend sticking with the GSON library to parse JSON. Here's what a Volley request with built-in JSON processing looks like:

 import java.io.UnsupportedEncodingException; 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 com.android.volley.toolbox.HttpHeaderParser; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; /** * Volley GET request which parses JSON server response into Java object. */ public class GsonRequest<T> extends Request<T> { /** JSON parsing engine */ protected final Gson gson; /** class of type of response */ protected final Class<T> clazz; /** result listener */ private final Listener<T> listener; public GsonRequest(String url, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) { super(Method.GET, url, errorListener); this.clazz = clazz; this.listener = listener; this.gson = new Gson(); } @Override protected void deliverResponse(T response) { listener.onResponse(response); } @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String json = new String( response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success( gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JsonSyntaxException e) { return Response.error(new ParseError(e)); } } } 

Suppose you have a server method located at http://example.com/api/persons/ that returns an array of JSON Person; A person looks like this:

 public class Person { String firstName; String lastName; } 

We can call the above method as follows:

 GsonRequest<Person[]> getPersons = new GsonRequest<Person[]>("http://example.com/api/persons/", Person[].class, new Listener<Person[]>() { @Override public void onResponse(Person[] response) { List<Person> persons = Arrays.asList(response); // TODO deal with persons } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO deal with error } }); VolleyQueue.get().add(getPersons); 

And finally, in the response listener, we get an array of Person, which can be converted to a list and sent to the ListView adapter.

+38
source share

you can use the gson project to work with json in java and android application, it's so simple this link is a sample link to use gson

How to convert Java object to JSON (Gson)

0
source share

You can use your own JSONParsor in the Android structure, your requirements are relatively less, and JSON is simple . Here is the link for the tutorial.

But if you use complex JSON objects, use libraries like

  • GSON
  • Jackson

I find GSON easier and more efficient. You know more about this from here.

0
source share

All Articles