title: Response handling with Volley
body: <p>I am using <code>Volley</code>in my project for handling network requests. Here is a sample <code>JSON</code> my server returns </p>
  
  <p><strong>JSON Object Response</strong> </p>
  
  <pre><code>{"code":"success", "data":{"some data"}}
  </code></pre>
  
  <p><strong>JSON Array Response</strong></p>
  
  <pre><code>{"code":"success", "data":["some data"]}
  </code></pre>
  
  <p>When some validation error or any other error occurs, server returns following response:</p>
  
  <pre><code>{"code":"failed", "error":"Access denied"}
  </code></pre>
  
  <p>The problem is with parsing data. when request is successful, in <code>onResponse</code> of <code>ResponseListener</code>, I simply get the content of <code>data</code> key. Where as, I was expecting the result same as what I posted above. I am not getting why <code>Volley</code> is returning only content of <code>data</code> and not complete <code>JSON</code>. I had used <code>Volley</code> earlier also. But never faced such type of problem. </p>
  
  <p><strong>Parsing Code:</strong></p>
  
  <pre><code>private void getOnboardingCategories() {
      Response.Listener&lt;JSONArray&gt; responseListener = new Response.Listener&lt;JSONArray&gt;() {
  
          @Override
          public void onResponse(JSONArray response) {
              Log.d(LOG_TAG, "CATEGORY RESPONSE: " + response.toString());
              if (response != null) {
                  int dataLength = response.length();
                  for (int i = 0; i &lt; dataLength; i++) {
                      JSONObject jObject = response.optJSONObject(i);
                      if (jObject != null) {
                          CategoryType2 categoryType2 = new CategoryType2();
                          categoryType2.set_id(jObject.optString("_id"));
                          categoryType2.setName(jObject.optString("name"));
                          categoryType2.setApp_icon_data(jObject.optString("thumbnail_data"));
                          categories.add(categoryType2);
                      }
                  }
              }
              if (isVisible())
                  sellAdapter.notifyDataSetChanged();
          }
      };
  
      Response.ErrorListener errorListener = new Response.ErrorListener() {
  
          @Override
          public void onErrorResponse(VolleyError error) {
              error.printStackTrace();
              Util.errorHandler(error, ctx);
          }
      };
  
      JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Method.GET, url,
              null, responseListener, errorListener);
      MyApplication.getInstance().addToRequestQueue(jsonObjectRequest, "onboarding");
  }
  </code></pre>
  
  <p><strong>Response on Success:</strong></p>
  
  <pre><code>{
     code: "success",
     data: [
               {
                   _id: "55c06b05a3e0041a73cea744",
                   name: "Test Category 1",
                   thumbnail_data: "",
               },
               {
                   _id: "55c06b16a3e0046108cea744",
                   name: "Test Category 2",
                   thumbnail_data: "",
               }
           ]
  }
  </code></pre>
  
  <p>In <code>onResponse</code> of <code>ResponseListener</code>, I get this data:</p>
  
  <pre><code>[
      {
          _id: "55c06b05a3e0041a73cea744",
          name: "Test Category 1",
          thumbnail_data: "",
      },
      {
          _id: "55c06b16a3e0046108cea744",
          name: "Test Category 2",
          thumbnail_data: "",
      }
  ]
  </code></pre>
  
  <p>When error occurs, server returns this response:</p>
  
  <pre><code>{"code":"failed", "error":"error_msg"}
  </code></pre>
  
  <p>Due to this, <code>Volley</code> throws <code>ParseException</code> as it expects <code>JSONArray</code>. I need to show the error message to the user. Earlier, I was using <code>AsyncTask</code> and I handled the error there. But, with <code>Volley</code> I am facing difficulty. I looked into <code>VolleyError</code>, but didn't got any clue. </p>
  
  <p><strong>Update 1</strong></p>
  
  <pre><code>private void getOnboardingCategories() {
      showSpinnerDialog(true);
      Response.Listener&lt;JSONObject&gt; responseListener = new Response.Listener&lt;JSONObject&gt;() {
  
          @Override
          public void onResponse(JSONObject response) {
              Log.d(LOG_TAG, "CATEGORY RESPONSE: " + response.toString());
              hideSpinnerDialog();
              String code = response.optString("code");
              if (code.equals("success")) {
                  if (response != null) {
                      JSONArray dataArray = response.optJSONArray("data");
                      int dataLength = dataArray.length();
                      for (int i = 0; i &lt; dataLength; i++) {
                          JSONObject jObject = dataArray.optJSONObject(i);
                          if (jObject != null) {
                              CategoryType2 categoryType2 = new CategoryType2();
                              categoryType2.set_id(jObject.optString("_id"));
                              categoryType2.setName(jObject.optString("name"));
                              categoryType2.setApp_icon_data(jObject.optString("app_icon_data"));
                              categories.add(categoryType2);
                          }
                      }
                  }
              }
              if (isVisible())
                  sellAdapter.notifyDataSetChanged();
          }
      };
  
      Response.ErrorListener errorListener = new Response.ErrorListener() {
  
          @Override
          public void onErrorResponse(VolleyError error) {
              error.printStackTrace();
              Util.errorHandler(error, ctx);
          }
      };
  
      JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.GET, url,
              null, responseListener, errorListener);
      MyApplication.getInstance().addToRequestQueue(jsonObjectRequest, "onboarding");
  }
  </code></pre>
  
  <p><strong>Update</strong>
  This issue was not about <code>Volley</code>. There was issue on the server end wrt gzip compression. I am going to vote for closing this question.</p>