RequestQueue queue = Volley.newRequestQueue(this); final String url = "http://www.mywebsite.com/test.php"; // prepare the Request JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // display response Log.d("Response", response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("Error.Response", "test"); } } ); //to clear the cache queue.getCache().clear(); // add it to the RequestQueue queue.add(getRequest);
@Override public void onResponse(String response) { if (mBuilder.allowNullResponse && response.length() == 0 || response.equalsIgnoreCase("null") || response.equalsIgnoreCase("[]")) { sendResponse(null); return; } try { JSONArray jsonArray = new JSONArray(response); if (jsonArray.length() > 0) { sendResponse(jsonArray); } else { sendFinish(R.string.parsing_error, new ParseError()); } } catch (JSONException e) { sendFinish(R.string.network_error, new ParseError(e)); if (mBuilder.shouldCache && mQueue.getCache() != null) mQueue.getCache().clear(); } }
@Override protected com.android.volley.Response<JSONObject> parseNetworkResponse(NetworkResponse response) { mResponse = response; try { if (!(response != null && response.data != null)) return com.android.volley.Response.error(null); String jsonString = new String(response.data, mBuilder.encoding); if (jsonString.length() == 0 || jsonString.equalsIgnoreCase("null")) { if (mBuilder.allowNullResponse) { return com.android.volley.Response.success(null, getCacheEntry(response)); } } return com.android.volley.Response.success(new JSONObject(jsonString), getCacheEntry(response)); } catch (UnsupportedEncodingException e) { return com.android.volley.Response.error(new ParseError(e)); } catch (JSONException je) { if (mBuilder.shouldCache && mQueue.getCache() != null) mQueue.getCache().clear(); return com.android.volley.Response.error(new ParseError(je)); } } };