android - Response handling with Volley -
i using volley
in project handling network requests. here sample json
server returns
json object response
{"code":"success", "data":{"some data"}}
json array response
{"code":"success", "data":["some data"]}
when validation error or other error occurs, server returns following response:
{"code":"failed", "error":"access denied"}
the problem parsing data. when request successful, in onresponse
of responselistener
, content of data
key. as, expecting result same posted above. not getting why volley
returning content of data
, not complete json
. had used volley
earlier also. 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(); (int = 0; < 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
, data:
[ { _id: "55c06b05a3e0041a73cea744", name: "test category 1", thumbnail_data: "", }, { _id: "55c06b16a3e0046108cea744", name: "test category 2", thumbnail_data: "", } ]
when error occurs, server returns response:
{"code":"failed", "error":"error_msg"}
due this, volley
throws parseexception
expects jsonarray
. need show error message user. earlier, using asynctask
, handled error there. but, volley
facing difficulty. looked volleyerror
, didn't got 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(); (int = 0; < 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 issue not volley
. there issue on server end wrt gzip compression. going vote closing question.
but, when error occurs, parse exception, when making request jsonarray
use jsonobject. has() , jsonobject. isnull() check key present in json response before parsing json.
for example:
jsonobject jsonobject=new jsonobject(<server_response_string>); if(jsonobject.has("data") && !jsonobject.isnull("data")) { // data jsonarray response }else{ // message using error key }
Comments
Post a Comment