How can i post a complex JSON object to a server via POST in android? -


i have complex json objects, want post server via http post request. have seen many examples volley library, work simple key-value pairs (hashmap). can advice library handles posting of complex json objects?

i don't know reference of library works jsonobjcet have working code asynctask here ,which sending jsonobjects server :

    private class backgroundoperation extends asynctask<string, void, string> {          @override         protected string doinbackground(string... params)              //your network connection code should here .             string response = postcall("put webservice url here");             return response ;         }          @override         protected void onpostexecute(string result) {             //print response here .             log.d("post response",result);          }          @override         protected void onpreexecute() {}          @override         protected void onprogressupdate(void... values) {}     }          public static string postcall(jsonobject josnobj) {         string result ="";         try {             //connect             httpurlconnection urlconnection = (httpurlconnection) ((new url(uri).openconnection()));             urlconnection.setdooutput(true);             urlconnection.setrequestproperty("content-type", "application/json");             urlconnection.setrequestproperty("accept", "application/json");             urlconnection.setrequestmethod("post");             urlconnection.connect();             //write             outputstream outputstream = urlconnection.getoutputstream();             bufferedwriter writer = new bufferedwriter(new outputstreamwriter(outputstream, "utf-8")); //call parserusuariojson() inside write(),make sure returning proper json string .             writer.write(josnobj.tostring());             writer.close();             outputstream.close();              //read             bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(urlconnection.getinputstream(), "utf-8"));             string line = null;             stringbuilder sb = new stringbuilder();             while ((line = bufferedreader.readline()) != null) {                 sb.append(line);             }             bufferedreader.close();             result = sb.tostring();         } catch (unsupportedencodingexception e){             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }         return result;     } 

now can call above from activity's oncreate() function using below code .

   jsonobject jobj = new jsonobject(); jobj.put("name","yourname"); jobj.put("email","mail"); jobj.put("pass","pass");     new backgroundoperation().execute(jobj.tostring()); 

note : don't forget mention below permission in manifest.xml

<uses-permission android:name="android.permission.internet" />  

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -