android - Getting empty array [] response with volley Json -
i getting data api web server call. if use basic web call (with httpget) able data expecting. using volley json getting empty array [] response.
i following jsonarray response.
[ { "nickname":"panikos", "username":"panikos@gmail.com", "user_type":"leader", "latest_steps":"2" }, { "nickname":"nikki", "username":"nikki@gmail.com", "user_type":"master", "latest_steps":"3" }, ........... ........... ........... ........... ]
here code data server using volley
jsonarrayrequest arrreq = new jsonarrayrequest(lurl, new response.listener<jsonarray>() { public void onresponse(jsonarray arg0) { log.d("debug","json array"+arg0.tostring()); mlistener.notifyresponse(arg0.tostring()); }; }, new response.errorlistener() { @override public void onerrorresponse(volleyerror arg0) { log.d("debug", "error response"+arg0.tostring()); if (arg0.networkresponse == null) { if (arg0.getclass().equals(timeouterror.class)) { // show timeout error message mlistener.notifyconnection("oops.timeout! please check connection."); } }else{ mlistener.notifyerror(arg0); } } }) { public map<string, string> getheaders() throws authfailureerror { hashmap<string, string> params = new hashmap<string, string>(); string creds = string.format("%s:%s",xippxx,xxcvxx); string auth = "basic " + base64.encodetostring(creds.getbytes(), base64.no_wrap); params.put("authorization", auth); return params; } }; appcontroller.getinstance().adtorequestqure(arrreq);
by using code every time getting empty array [] in volley json response. tried same basic http call (by using asynctask) giving proper data expecting.
this basic code getting data normal web request call::
@override protected void doinbackground(string... params) { try { httpget request=new httpget(params[0]); defaulthttpclient httpclient=new defaulthttpclient(); string creds = string.format("%s:%s","xippxx","xxcvxx"); string auth = "basic " + base64.encodetostring(creds.getbytes(), base64.no_wrap); request.setheader("authorization", auth); httpresponse response=httpclient.execute(request); httpentity entity=response.getentity(); string jsondata = entityutils.tostring(entity); log.d("debug", "json data in asynctask:: "+jsondata); jsonarray ljsonarray=new jsonarray(jsondata); log.d("debug", "json array data:: "+ljsonarray.tostring()); } catch (exception e) { e.printstacktrace(); } return null; }
this code working perfectly.. glad know making mistake in volley json ? tried many scenario's (those can't shown here) stringrequest, , other things base64.default no luck.
sorry poor english , long question. 1 can point out me.. thank !!!
i had similar problem, root of issue you're trying perform params, it's different post, need modify request.
this general template might you, try request (with modifications param..):
edited:
map<string, object> params = new hashmap<>() ; string creds = string.format("%s:%s",xippxx,xxcvxx); string auth = "basic " + base64.encodetostring(creds.getbytes(), base64.no_wrap); params.put("authorization", auth); string urlwithparams = creategetwithparams(originalurl, params); decodedstringrequest request = new decodedstringrequest(request.method.get, urlwithparams, new response.listener<string>() { @override public void onresponse(string response) { log.d(tag + ": ", "volley response: " + response.tostring()); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { if (null != error.networkresponse) { log.d(tag + ": ", "error response code: " + error.networkresponse.statuscode); } } }); requestqueue.add(request);
and here check response here:
public class decodedstringrequest extends stringrequest { private static final string tag = "decodedstringrequest"; private final response.listener<string> mlistener; public decodedstringrequest(int method, string url, response.listener<string> listener, response.errorlistener errorlistener) { super(method,url, listener, errorlistener); mlistener = listener; } @override protected response<string> parsenetworkresponse(networkresponse response) { try { //add more of magic here if needed... maybe print data see string responsebody = new string(response.data, "utf-8"); return (response.success(responsebody, getcacheentry())); } catch (unsupportedencodingexception e) { volleylog.e("unsupportedencodingexception"); log.d(tag +" networkresponse exception", e.getmessage() ); return (response.success("uploaded, problem url return", getcacheentry())); } } @override protected void deliverresponse(string response) { mlistener.onresponse(response); } }
oh and:
private string creategetwithparams(string url, map<string, object> params) { stringbuilder builder = new stringbuilder(); (string key : params.keyset()) { object value = params.get(key); if (value != null) { try { value = urlencoder.encode(string.valueof(value), http.utf_8); if (builder.length() > 0) builder.append("&"); builder.append(key).append("=").append(value); } catch (unsupportedencodingexception e) { log.d(tag + " creategetwithparams: ", e.getmessage()); } } } return (url + "?" + builder.tostring()); }
hope helps!
Comments
Post a Comment