Android: Http post with parameters not working -
i need create http post request parameters. know there many examples out there, have tried using httpparams, namevaluepair etc cant seem correct format server.
server type: rest based api utilizing json data transfer
content-type: application/json
accept: application/json
content-length: 47
{"username":"abcd","password":"1234"}
i can pass these headers cant seem pass these params "username","password". here code:
httpclient client = new defaulthttpclient(); httppost post = new httppost("http://www.mymi5.net/api/auth/login"); list<namevaluepair> pairs = new arraylist<namevaluepair>(); pairs.add(new basicnamevaluepair("username","abcd")); pairs.add(new basicnamevaluepair("password","1234")); post.setheader("content-type", "application/json"); post.setheader("accept", "application/json"); urlencodedformentity entity = new urlencodedformentity(pairs,"utf-8"); post.setentity(entity); httpresponse response = client.execute(post);
i tried debug, cant see if entity attached or not... doing wrong?
thanks in advance. maaz
try this:
httpclient client = new defaulthttpclient(); httppost post = new httppost("http://www.mymi5.net/api/auth/login"); post.setheader("content-type", "application/json"); post.setheader("accept", "application/json"); jsonobject obj = new jsonobject(); obj.put("username", "abcd"); obj.put("password", "1234"); post.setentity(new stringentity(obj.tostring(), "utf-8")); httpresponse response = client.execute(post);
Comments
Post a Comment