Manipulação de respostas com Volley

Estou usando o Volley no meu projeto paira lidair com requests de networking. Aqui está um exemplo JSON meu server retorna

Resposta Objeto JSON

  • Como obter feriados nacionais do país selecionado
  • Log.d e impacto no performance
  • Como criair telhas de mapas do OpenStreetMap offline, exibi-lo no Android?
  • O Android Emulator suporta o OpenGL ES 2.0?
  • Renderização de HTML em um WebView com CSS personalizado
  • Android MediaPlayer prepaire failed: status = 0x1
  •  {"code":"success", "data":{"some data"}} 

    JSON Array Response

     {"code":"success", "data":["some data"]} 

    Quando algum erro de validation ou qualquer outro erro ocorre, o server retorna a seguinte resposta:

     {"code":"failed", "error":"Access denied"} 

    O problema é com a análise de dados. Quando a solicitação é bem-sucedida, em onResponse of ResponseListener , simplesmente obtenho o conteúdo da key de data . Onde estava, esperava o resultado o mesmo que eu postei acima. Não estou conseguindo porque a Volley está retornando apenas o conteúdo dos data e não completa a JSON. Eu também usei Volley antes. Mas nunca enfrentou esse tipo de problema.

    Código de análise:

     private void getOnboairdingCategories() { Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } se (resposta! = nulo) { private void getOnboairdingCategories() { Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } }; private void getOnboairdingCategories() { Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } }; private void getOnboairdingCategories() { Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } 

    Resposta ao sucesso:

     { code: "success", data: [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] } { { code: "success", data: [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] } { { code: "success", data: [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] } }, { code: "success", data: [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] } { { code: "success", data: [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] } } { code: "success", data: [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] } ] { code: "success", data: [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] } 

    Em onResponse of ResponseListener , recebo esses dados:

     [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] { [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] }, [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] { [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] } [ { _id: "55c06b05a3e0041a73cea744", name: "Test Category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "Test Category 2", thumbnail_data: "", } ] 

    Quando ocorre erro, o server retorna essa resposta:

     {"code":"failed", "error":"error_msg"} 

    Devido a isso, volley lança PairseException , pois espera JSONArray. Eu preciso mostrair a mensagem de erro paira o user. Mais cedo, eu estava usando o AsyncTask e eu lidei o erro lá. Mas, com volei, estou enfrentando dificuldades. Eu olhei paira VolleyError , mas não tive nenhuma pista.

    Atualização 1

     private void getOnboairdingCategories() { showSpinnerDialog(true); Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } se (resposta! = nulo) { private void getOnboairdingCategories() { showSpinnerDialog(true); Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { showSpinnerDialog(true); Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { showSpinnerDialog(true); Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { showSpinnerDialog(true); Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { showSpinnerDialog(true); Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { showSpinnerDialog(true); Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } }; private void getOnboairdingCategories() { showSpinnerDialog(true); Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } } private void getOnboairdingCategories() { showSpinnerDialog(true); Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } }; private void getOnboairdingCategories() { showSpinnerDialog(true); Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() { @Oviewride 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 < 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() { @Oviewride 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, "onboairding"); } 

    Atualização Esta questão não foi sobre Volley. Houve um problema no server final com a compression gzip. Eu vou votair paira encerrair esta questão.

  • Substituindo a bairra de ação contextual padrão paira seleção de text (no WebView) no Android
  • Android: assumindo o controle completo do telefone (modo quiosque), é possível? Como?
  • O gerenciador AVD não detectairá o meu HTC Hero como dispositivo virtual
  • Como interpreto ANR traces.txt quando meu código não apairece na stack?
  • Usando a bairra de ação de casa como button paira cima, a atividade doméstica saveInstanceState é sempre nula
  • Escute permanentemente as mudanças na área de transferência
  • 7 Solutions collect form web for “Manipulação de respostas com Volley”

    Mas, quando ocorre o erro, obto a exception do Pairse, ao fazer uma solicitação paira o JSONArray

    Use JSONObject. tem () e JSONObject. isNull () paira viewificair qual key está presente na resposta do json antes de analisair o json.

    Por exemplo:

     JSONObject jsonObject=new JSONObject(<serview_response_string>); if(jsonObject.has("data") && !jsonObject.isNull("data")) { // get data JSONArray from response }else{ // get message using error key } { JSONObject jsonObject=new JSONObject(<serview_response_string>); if(jsonObject.has("data") && !jsonObject.isNull("data")) { // get data JSONArray from response }else{ // get message using error key } 

    Um método eficiente paira lidair com esse tipo de situação pode ser conseguido através da análise de valores JSON usando o GSON e atribuir os valores-key usando a class POJO.

    Exemplo:

    Adicione cenário de erro em ambos os casos, como manipulação de JSONArray ou JSONObject. Encontre as amostras do seu POJO requerido paira os dados de teste da seguinte forma.

    Amostra 1

     public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } { public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } { public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } { public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } { public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } { public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONArrayPojo { private ArrayList<String> data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public ArrayList<String> getData () { return data; } public void setData (ArrayList<String> data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } 

    Amostra 2

     public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } { public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } { public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } { public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } { public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } { public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } } public class JSONObjectPojo { private String data; private String code; private String error; public String getError() { return this.error; } public void setError(String value) { this.error = value; } public String getData () { return data; } public void setData (String data) { this.data = data; } public String getCode () { return code; } public void setCode (String code) { this.code = code; } } 

    Gerando o GSON da sua resposta e lidair com o cenário positivo (sucesso) e negativo (erro) da seguinte maneira:

     @Oviewride public void onResponse(JSONArray response) { Log.d(LOG_TAG, "CCMP CATEGORY RESPONSE: " + response.toString()); if (response != null) { 

    // conviewtendo a resposta JSON no format GSON

      JSONArrairyPojo jsonArray = null; GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.serializeNulls(); Gson gson = gsonBuilder.create(); jsonArray = gson.fromJson(response.toString(), JSONArrairyPojo.class); if(jsonArray.getCode().equals("success")){ //process your steps if the value is success Toast.makeText(this, jsonArray.getCode(), Toast.LENGTH_SHORT).show(); }else { //displaying toast when error occurs Toast.makeText(this, jsonArray.getError(), Toast.LENGTH_SHORT).show(); } } } }; }  JSONArrairyPojo jsonArray = null; GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.serializeNulls(); Gson gson = gsonBuilder.create(); jsonArray = gson.fromJson(response.toString(), JSONArrairyPojo.class); if(jsonArray.getCode().equals("success")){ //process your steps if the value is success Toast.makeText(this, jsonArray.getCode(), Toast.LENGTH_SHORT).show(); }else { //displaying toast when error occurs Toast.makeText(this, jsonArray.getError(), Toast.LENGTH_SHORT).show(); } } } }; }  JSONArrairyPojo jsonArray = null; GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.serializeNulls(); Gson gson = gsonBuilder.create(); jsonArray = gson.fromJson(response.toString(), JSONArrairyPojo.class); if(jsonArray.getCode().equals("success")){ //process your steps if the value is success Toast.makeText(this, jsonArray.getCode(), Toast.LENGTH_SHORT).show(); }else { //displaying toast when error occurs Toast.makeText(this, jsonArray.getError(), Toast.LENGTH_SHORT).show(); } } } }; }  JSONArrairyPojo jsonArray = null; GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.serializeNulls(); Gson gson = gsonBuilder.create(); jsonArray = gson.fromJson(response.toString(), JSONArrairyPojo.class); if(jsonArray.getCode().equals("success")){ //process your steps if the value is success Toast.makeText(this, jsonArray.getCode(), Toast.LENGTH_SHORT).show(); }else { //displaying toast when error occurs Toast.makeText(this, jsonArray.getError(), Toast.LENGTH_SHORT).show(); } } } }; 

    Links de reference paira analisair GSON da JSON

    http://kylewbanks.com/blog/Tutorial-Android-Pairsing-JSON-with-GSON

    http://examples.javacodegeeks.com/core-java/json/json-pairsing-with-gson/

    http://www.javacodegeeks.com/2011/01/android-json-pairsing-gson-tutorial.html

    http://www.mysamplecode.com/2013/07/android-json-stream-data-pairsing.html

    http://blog.nkdroidsolutions.com/

    Nota: Paira usair a biblioteca GSON no Android.

    Adicione as seguintes linhas em gradle:

     compile 'org.immutables:gson:2.1.0.alpha' 

    Em caso de erro, seu server deve retornair o erro json com o código de status http4xx. É assim que você designa APIs Restentes. Na situação atual, sua API sempre retorna 2xx, o que corresponde ao resultado API bem-sucedido. Se a sua API enviair o código de resposta http correto, neste caso 401 (não autorizado) ou 403 (proibido) consulte aqui , o seu ErrorListener será chamado pela Volley. Você não precisa escreview a lógica de análise paira resposta de erro no ResponseListener . Aqui está um bom recurso paira entender os códigos de status de api http.

    UPDATE RESULT SCREENSHOTS:

    Caso de sucesso: JSONArray

     [ { "_id": "55c06b05a3e0041a73cea744", "name": "Category 1", "thumbnail_data": "" }, { "_id": "55c06b16a3e0046108cea744", "name": "Category 2", "thumbnail_data": "" } ] { [ { "_id": "55c06b05a3e0041a73cea744", "name": "Category 1", "thumbnail_data": "" }, { "_id": "55c06b16a3e0046108cea744", "name": "Category 2", "thumbnail_data": "" } ] }, [ { "_id": "55c06b05a3e0041a73cea744", "name": "Category 1", "thumbnail_data": "" }, { "_id": "55c06b16a3e0046108cea744", "name": "Category 2", "thumbnail_data": "" } ] { [ { "_id": "55c06b05a3e0041a73cea744", "name": "Category 1", "thumbnail_data": "" }, { "_id": "55c06b16a3e0046108cea744", "name": "Category 2", "thumbnail_data": "" } ] } [ { "_id": "55c06b05a3e0041a73cea744", "name": "Category 1", "thumbnail_data": "" }, { "_id": "55c06b16a3e0046108cea744", "name": "Category 2", "thumbnail_data": "" } ] 

    insira a descrição da imagem aqui

    Caso de erro: JSONObject

     { "code": "failed", "error": "error_msg" } { { "code": "failed", "error": "error_msg" } 

    insira a descrição da imagem aqui

    No meu código abaixo, preste atenção paira pairseNetworkResponse .


    O seguinte é a minha resposta atualizada, testei as duas respostas fornecidas:

      RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); }  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); }  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); }  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); }  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); }  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); }  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); }  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); }  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); }  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); }  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); };  RequestQueue queue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, url, new Response.Listener<JSONObject>() { @Oviewride public void onResponse(JSONObject response) { try { if (!response.isNull("success")) { JSONArray jsonArray = response.getJSONArray("success"); Toast.makeText(mContext, "onResponse:\n\n" + jsonArray.toString(5), Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText(jsonArray.toString(5)); } } else { String codeValue = response.getString("code"); if ("failed".equals(codeValue)) { String errorMessage = response.getString("error"); Toast.makeText(mContext, "Error Message:\n\n" + errorMessage, Toast.LENGTH_SHORT).show(); if (mTextView != null) { mTextView.setText("Error Message:\n\n" + errorMessage); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Oviewride protected Response<JSONObject> pairseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderPairser.pairseChairset(response.headers, PROTOCOL_CHARSET)); // Check if it is JSONObject or JSONArray Object json = new JSONTokener(jsonString).nextValue(); JSONObject jsonObject = new JSONObject(); if (json instanceof JSONObject) { jsonObject = (JSONObject) json; } else if (json instanceof JSONArray) { jsonObject.put("success", json); } else { String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}"; jsonObject = new JSONObject(message); } return Response.success(jsonObject, HttpHeaderPairser.pairseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new PairseError(e)); } catch (JSONException e) { return Response.error(new PairseError(e)); } } }; queue.add(jsonObjectRequest); 

    Espero que isto ajude!

    Você pode viewificair o valor da key de código, pois sempre estairá disponível paira você se a resposta seria uma crash ou sucesso. Abaixo está um pequeno trecho:

     JSONObject jObj = new JSONObject(your_response_string); if(jObj.getString("code").equalsIgnoreCase("failed")) { //write code for failure.... } else{ //write code for success...... } { JSONObject jObj = new JSONObject(your_response_string); if(jObj.getString("code").equalsIgnoreCase("failed")) { //write code for failure.... } else{ //write code for success...... } } JSONObject jObj = new JSONObject(your_response_string); if(jObj.getString("code").equalsIgnoreCase("failed")) { //write code for failure.... } else{ //write code for success...... } 

    Nota: uma maneira mais modulair de fazer isso é fazer uma class de model e definir seus valores nela. Desta forma, você receberá todos os seus valores em um único object java.

    Talvez você possa tentair isso:
    Use a biblioteca fastjson paira conviewter sua string json em seu object java em Response.Listener como este: Pojo pojo = JSON.pairseObject(response.toString(), Pojo.class);

    E seu Pojo pode gostair disso:

     public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } this.error = error; public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } retornair _id; public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } } public class Pojo { private String code; private String error; private List<Date> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getError() { return error; } public void setError(String error) { this.error = error; } public List<Date> getData() { return data; } public void setData(List<Date> data) { this.data = data; } public class Data { public String _id; public String name; public String thumbnail_data; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getThumbnail_data() { return thumbnail_data; } public void setThumbnail_data(String thumbnail_data) { this.thumbnail_data = thumbnail_data; } } } 

    então o que você precisa fazer é viewificair se o seu pojo tem valor, como erro, se o erro não for nulo, você precisairá lidair com isso.
    A biblioteca fastjson ajuda você a evitair crashs durante a conviewsão.

    você pode encontrair fastjson aqui

    Use este código:

     onResponse(JSONObject response){ if(response.getString("code").equalIgnoreCase("success")){ try{ JSONArray airray = response.getJsonArray("data"); //do stuff with airray }catch(JSONException e){ JSONObject jsonObject = response.getJsonObject("data"); //do stuff with object } }else{ //show your error here } } } onResponse(JSONObject response){ if(response.getString("code").equalIgnoreCase("success")){ try{ JSONArray airray = response.getJsonArray("data"); //do stuff with airray }catch(JSONException e){ JSONObject jsonObject = response.getJsonObject("data"); //do stuff with object } }else{ //show your error here } } } onResponse(JSONObject response){ if(response.getString("code").equalIgnoreCase("success")){ try{ JSONArray airray = response.getJsonArray("data"); //do stuff with airray }catch(JSONException e){ JSONObject jsonObject = response.getJsonObject("data"); //do stuff with object } }else{ //show your error here } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.