Please enable JavaScript to view the comments powered by Disqus.

Parse JSON Request using Volley and GSON

In previous post,  I showed you how to use Volley to make a simple request in Android application.

This time, I’ll show you the recommended way to make a JSON request using Volley and how to customize Volley request to take advantage of the GSON library.

A - Setting up a RequestQueue

Firstly, create the VolleyHelper class. This will be a singleton class where we store and init all core Volley objects:

public class VolleyHelper {
    private static VolleyHelper INSTANCE;
    private RequestQueue requestQueue;
    private Context context;

    private VolleyHelper(Context context){
        this.context = context;
        requestQueue = getRequestQueue();
    }

    public static synchronized VolleyHelper getInstance(Context context){
        if(INSTANCE == null){
            INSTANCE = new VolleyHelper(context);
        }
        return INSTANCE;
    }

    public RequestQueue getRequestQueue(){
        if(requestQueue == null){
            requestQueue = Volley.newRequestQueue(context.getApplicationContext());
        }
        return requestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }
}

B - Make a JSON Request

Firstly, create the layout for our simple demo with one Button and one TextView to display the result:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button android:id="@+id/btnJSONRequest"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="JSON Request"/>

    <TextView android:id="@+id/tvResult"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnJSONRequest"/>

</RelativeLayout>

In the MainActivity.java, we declare all indeed variables for all the controls and the JSON url:

Button btnJSONRequest;
Button btnGSONRequest;
TextView tvResult;

String url="http://icetea09.com/blog-files/demo_json";

Make sure that you remember to inject all the variables to the proper controls in the layout:

btnJSONRequest = (Button)findViewById(R.id.btnJSONRequest);
btnGSONRequest = (Button)findViewById(R.id.btnGSONRequest);
tvResult = (TextView)findViewById(R.id.tvResult);

Then, the most important step is creating a JsonObjectRequest:

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

		@Override
		public void onResponse(JSONObject response) {
			tvResult.setText("Response: " + response.toString());
			String textResult = "";
			try {
				JSONArray arrProducts = response.getJSONArray("products");
				for(int i=0; i<arrProducts.length(); i++){
					JSONObject productItem = (JSONObject)arrProducts.get(i);
					textResult += "Name: " + productItem.getString("name") + "\n";
					textResult += "Description: " + productItem.getString("description") + "\n";
					textResult += "Price: $" + productItem.getString("price") + "\n\n";
				}
				tvResult.setText(textResult);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}, new Response.ErrorListener() {

		@Override
		public void onErrorResponse(VolleyError error) {
			if(error != null) Log.e("MainActivity", error.getMessage());

		}
	});

And add that JsonObjectRequest to RequestQueue in the Button click event:

btnJSONRequest.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
		VolleyHelper.getInstance(getApplicationContext()).addToRequestQueue(jsObjRequest);
	}
});

Run the project and enjoy the result:

Screenshot_2014-11-02-13-28-52

C - Custom the Request with GSON

In this section, I’ll describe how to implement a custom request types, for types that don’t have out-of-the-box Volley support - GSON Request.

As you know that GSON is a very famous and effective library used for JSON converting. Follow this link if you want to learn more about GSON.

If you want to implement a custom request, you have to do 2 steps:

-     Extend the Request class, where represents the type of parsed response the request expects</p>

-     Implement the abstract methods parseNetworkResponse() and deliverResponse()

Now apply all those steps to the GSONRequest class:
public class GsonRequest<T> extends Request<T> {
    private final Gson gson = new Gson();
    private final Class<T> clazz;
    private final Map<String, String> headers;
    private final Response.Listener<T> listener;

    public GsonRequest(String url, Class<T> clazz, Map<String, String> headers,
                       Response.Listener<T> listener, Response.ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.clazz = clazz;
        this.headers = headers;
        this.listener = listener;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return headers != null ? headers : super.getHeaders();
    }

    @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));
        }
    }

    @Override
    protected void deliverResponse(T response) {
        listener.onResponse(response);
    }
}
The way to use GSONRequest is very familiar to normal requests. First, we need to create all the models class that equivalent to the JSON response: Product.java
public class Product {
    private String id;
    private String name;
    private String description;
    private String price;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}
Products.java
public class Products {
    private List<Product> products;

    public List<Product> getProducts() {
        return products;
    }
}
In MainActivity.java, we create a GSONRequest object:
final GsonRequest gsonRequest = new GsonRequest(url, Products.class, null, new Response.Listener<Products>() {

	@Override
	public void onResponse(Products products) {
		String textResult = "";
		for(int i=0; i<products.getProducts().size(); i++){
			Product productItem = products.getProducts().get(i);
			textResult += "Name: " + productItem.getName() + "\n";
			textResult += "Description: " + productItem.getDescription() + "\n";
			textResult += "Price: $" + productItem.getPrice() + "\n\n";
		}
		tvResult.setText(textResult);
	}
}, new Response.ErrorListener() {
	@Override
	public void onErrorResponse(VolleyError volleyError) {
		if(volleyError != null) Log.e("MainActivity", volleyError.getMessage());
	}
});
Then, add that GSONRequest object to the RequestQueue:
btnGSONRequest.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
		VolleyHelper.getInstance(getApplicationContext()).addToRequestQueue(gsonRequest);
	}
});
Finally, run the project and enjoy the result: Screenshot_2014-11-02-14-01-16 As you can see, I added one more button called GSON Request. That's all for this post, hope you enjoy this! :D