Volley send JSONObject to server with POST method -


i want send jsonobject below format server using volley library { "user_id": 12, "answers": { "11": 3, "12": 4, "13": 5 } }

jsonobject object = new jsonobject();              try {                 object.put("user_id", user_id);                 jsonobject answers = new jsonobject();                (int = 0; < questions.size(); i++) {                     jsonobject answer = new jsonobject();                     answer.put(questions.get(i).getid(),questions.get(i).getanswer());                     answers.put("answers", answer);                     object.put("answers", answer);                 }             } catch (jsonexception e) {                 e.printstacktrace();             } 

if want use stringrequest how should send jsonobject server using post method

you can use following working sample code. have tested. hope helps!

       try {             jsonbody = new jsonobject();             jsonbody.put("title", "volleyapp android demo");             jsonbody.put("author", "bnk");             jsonbody.put("date", "2015/08/26");             requestbody = jsonbody.tostring();              stringrequest stringrequest = new stringrequest(1, url, new response.listener<string>() {                 @override                 public void onresponse(string response) {                     textview.settext(response);                 }             }, new response.errorlistener() {                 @override                 public void onerrorresponse(volleyerror error) {                     textview.settext(error.tostring());                 }             }) {                 @override                 public string getbodycontenttype() {                     return string.format("application/json; charset=utf-8");                 }                  @override                 public byte[] getbody() throws authfailureerror {                     try {                         return requestbody == null ? null : requestbody.getbytes("utf-8");                     } catch (unsupportedencodingexception uee) {                         volleylog.wtf("unsupported encoding while trying bytes of %s using %s",                                 requestbody, "utf-8");                         return null;                     }                 }             };             mysingleton.getinstance(this).addtorequestqueue(stringrequest);         } catch (jsonexception e) {             e.printstacktrace();         } 

update: create jsonobject requirement, use following:

        jsonobject jsonobject = new jsonobject();         try {             jsonobject.put("11", 3);             jsonobject.put("12", 4);             jsonobject.put("13", 5);              jsonobject jsonobject2 = new jsonobject().put("answers", jsonobject);             jsonobject2.put("user_id", 12);                     } catch (jsonexception e) {             e.printstacktrace();         } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -