android - Volley JsonArrayRequest Post request is not working -


i new volley , i'm trying make post request work

jsonarrayrequest

from many answers i've found on stackoverflow, tried follows -

    private void getcounts() {     string url = "http://192.168.1.100/json/count.php";     jsonarrayrequest req = new jsonarrayrequest(url,             new response.listener<jsonarray>() {                 @override                 public void onresponse(jsonarray response) {                     log.d(tag, response.tostring());                     hidepdialog();                 }             }, new response.errorlistener() {         @override         public void onerrorresponse(volleyerror error) {             volleylog.d(tag, "error: " + error.getmessage());             log.d(tag, "error: " + error.getmessage());             toast.maketext(getapplicationcontext(),                     error.getmessage(), toast.length_short).show();             hidepdialog();         }     }) {         @override         protected map<string, string> getparams() {             map<string, string> params = new hashmap<string, string>();             params.put("lastupdatedat", "0");             return params;         }          @override         public map<string, string> getheaders() throws authfailureerror {             hashmap<string, string> headers = new hashmap<string, string>();             headers.put("content-type", "application/json; charset=utf-8");             headers.put("user-agent", "my useragent");             return headers;         }          @override         public byte[] getbody() {             map<string, string> params = new hashmap<string, string>();             params.put("lastupdatedat", "0");              if (params != null && params.size() > 0) {                 log.d(tag, encodeparameters(params, getparamsencoding()).tostring());                 return encodeparameters(params, getparamsencoding());             }             return null;         }          protected byte[] encodeparameters(map<string, string> params, string paramsencoding) {             stringbuilder encodedparams = new stringbuilder();             try {                 (map.entry<string, string> entry : params.entryset()) {                     encodedparams.append(urlencoder.encode(entry.getkey(), paramsencoding));                     encodedparams.append('=');                     encodedparams.append(urlencoder.encode(entry.getvalue(), paramsencoding));                     encodedparams.append('&');                 }                 return encodedparams.tostring().getbytes(paramsencoding);             } catch (unsupportedencodingexception uee) {                 throw new runtimeexception("encoding not supported: " + paramsencoding, uee);             }         }      };      appcontroller.getinstance().addtorequestqueue(req); } 

as 1 can see, have tried both, getparams() , getbody() send lastupdatedat post request, no matter try value not posted , returns null on server.

i tried using jsonarrayrequest(request.method.post, ... , sending 'params' jsonobject in request, these 2 didn't work.

in 1 of similar question, use of following class suggested -

public class customjsonrequest extends request {  map<string, string> params;        private response.listener listener;   public customjsonrequest(int requestmethod, string url, map<string, string> params,                       response.listener responselistener, response.errorlistener errorlistener) {      super(requestmethod, url, errorlistener);     this.params = params;     this.listener = responselistener; }  @override protected void deliverresponse(object response) {     listener.onresponse(response);   }  @override public map<string, string> getparams() throws authfailureerror {          return params; }  @override protected response parsenetworkresponse(networkresponse response) {     try {         string jsonstring = new string(response.data, httpheaderparser.parsecharset(response.headers));         return response.success(new jsonobject(jsonstring),         httpheaderparser.parsecacheheaders(response));     } catch (unsupportedencodingexception e) {         return response.error(new parseerror(e));     } catch (jsonexception je) {         return response.error(new parseerror(je));     } }  } 

but couldn't make working response listner createrequestsuccesslistener , error listener.

in same similar question, following constructor suggested -

public jsonarrayrequest(int method, string url, jsonobject jsonrequest,         listener<jsonarray> listener, errorlistener errorlistener) {     super(method, url, (jsonrequest == null) ? null : jsonrequest.tostring(),          listener, errorlistener); } 

but, not figure out how make work?

try like:

        string url = "http://192.168.1.100/json/count.php";          final jsonobject _jsonrequest = new jsonobject();         _jsonrequest.put("key", value);          jsonarrayrequest _stringrequest = new jsonarrayrequest(          url , new response.listener<jsonarray>() {              @override             public void onresponse(jsonarray response) {                  // code here              }          }, new response.errorlistener() {              @override             public void onerrorresponse(volleyerror error) {                  // code here              }          }){              @override             public byte[] getbody() {                  try {                      return _jsonrequest.tostring().getbytes(getparamsencoding());                  } catch (unsupportedencodingexception uee) {                      return null;                  }              }              @override             public map<string, string> getheaders() throws authfailureerror {                  map<string, string> _params = new hashmap<string, string>();                  _params.put("content-type", "application/json");                  return _params;              }          };          appcontroller.getinstance().addtorequestqueue(_stringrequest); 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -