I am using Volley
in my project for handling network requests. Here is a sample JSON
my server returns
JSON Object Response
{"code":"success", "data":{"some data"}}
JSON Array Response
{"code":"success", "data":["some data"]}
When some validation error or any other error occurs, server returns following response:
{"code":"failed", "error":"Access denied"}
The problem is with parsing data. when request is successful, in onResponse
of ResponseListener
, I simply get the content of data
key. Where as, I was expecting the result same as what I posted above. I am not getting why Volley
is returning only content of data
and not complete JSON
. I had used Volley
earlier also. But never faced such type of problem.
Parsing Code:
private void getOnboardingCategories() {
Response.Listener<JSONArray> responseListener = new Response.Listener<JSONArray>() {
@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 < 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");
}
Response on Success:
{
code: "success",
data: [
{
_id: "55c06b05a3e0041a73cea744",
name: "Test Category 1",
thumbnail_data: "",
},
{
_id: "55c06b16a3e0046108cea744",
name: "Test Category 2",
thumbnail_data: "",
}
]
}
In onResponse
of ResponseListener
, I get this data:
[
{
_id: "55c06b05a3e0041a73cea744",
name: "Test Category 1",
thumbnail_data: "",
},
{
_id: "55c06b16a3e0046108cea744",
name: "Test Category 2",
thumbnail_data: "",
}
]
When error occurs, server returns this response:
{"code":"failed", "error":"error_msg"}
Due to this, Volley
throws ParseException
as it expects JSONArray
. I need to show the error message to the user. Earlier, I was using AsyncTask
and I handled the error there. But, with Volley
I am facing difficulty. I looked into VolleyError
, but didn't got any clue.
Update 1
private void getOnboardingCategories() {
showSpinnerDialog(true);
Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() {
@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 < 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");
}
Update
This issue was not about Volley
. There was issue on the server end wrt gzip compression. I am going to vote for closing this question.
update1
? – Phosphoprotein