java - Create a json string with List<NameValuePair> in android -
i want send http request url android. ios developer , trying learn android. used send json
string in ios below
{"function":"login", "parameters": {"username": "nithin""password": "123456"}}
how can send in android? tried list<namevaluepair>
can't find proper solution.
full code of tried -
httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); try { // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("function", "login")); namevaluepairs.add(new basicnamevaluepair("username", "nithin")); namevaluepairs.add(new basicnamevaluepair("password", "123456")); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block }
i think jsonstringer easy , useful here..
try this:
httppost request = new httppost(url); request.setheader("accept", "application/json"); request.setheader("content-type", "application/json"); jsonstringer vm; try { vm = new jsonstringer().object().key("function") .value(login).key("parameters").object() .key("username") .value(nithin).key("password").value("123456") .endobject().endobject(); stringentity entity = new stringentity(vm.tostring()); request.setentity(entity); httpclient httpclient = new defaulthttpclient(); httpresponse response = httpclient.execute(request);
Comments
Post a Comment