java - How to call Json Post request with queryparam on client side? -
i wrote both service , client part of application. tested service "postman
" application , working fine url = http://192.168.2.50:8084/facebusinessservice/webresources/service/login?phone=123456789&password=1234
however when try call on android application not working. while debuging on service side see phone , password parameters null.
here service side :
@path("login") @post @produces("application/json") public string postjson(@queryparam("phone")string phone, @queryparam("password") string password) { string info = null; try { userinfo userinfo = null; usermodel usermodel = new usermodel(); userinfo = usermodel.ispersonregistered(phone, password); gson gson = new gson(); system.out.println(gson.tojson(userinfo)); info = gson.tojson(userinfo); } catch (exception e) { system.out.println("exception: " + e.getmessage()); } return info; }
here android app side :
private userinfo loginuser(string phone, string password) { userinfo userinfo = null; httpclient httpclient = new defaulthttpclient(); httppost post = new httppost("http://192.168.2.27:8084/facebusinessservice/webresources/service/login"); try { /* multipartentity entity = new multipartentity(httpmultipartmode.browser_compatible); entity.addpart("phone", new stringbody(phone)); entity.addpart("password", new stringbody(password)); post.setentity(entity); */ list<namevaluepair> params = new arraylist<namevaluepair>(2); params.add(new basicnamevaluepair("phone", phone)); params.add(new basicnamevaluepair("password", password)); post.setentity(new urlencodedformentity(params, "utf-8")); log.d(tag, "post string: " + post.tostring()); try { httpresponse response = httpclient.execute(post); if (response.getentity().getcontentlength() > 0) { string json_string = entityutils.tostring(response.getentity()); jsonobject jsonobject = new jsonobject(json_string); // todo return userinfo; } } catch (ioexception e) { e.printstacktrace(); return null; } catch (jsonexception e) { e.printstacktrace(); return null; } } catch (unsupportedencodingexception e) { e.printstacktrace(); return null; } return null; }
i tried both multipartentity
, namevaluepair
none of them worked. give me idea how handle issue?
note when testing postman passed parameters (user name , password) part of url (url encoded), can directly retrieved on server side. (you don't need post request this). objects passed string objects, not json objects.
in client code , url different because you're encoding parameters part of post request entity (payload). parameters packaged inside of request/message body , not in url.
now since url doesn't have parameters, should retrieve them deserializing request (desderialize json request userinfo object).
note should rewrite server side code should accept application/json object apparently should return/produce string object (plain/text or application/html).
i'm not familiar gson code might like
@path("login") @post @produces("text/plain") @consumes("application/json") public string postjson(userinfo ui) { string info = null; try { userinfo userinfo = null; usermodel usermodel = new usermodel(); userinfo = usermodel.ispersonregistered(ui.phone, ui.password); gson gson = new gson(); system.out.println(gson.tojson(userinfo)); info = gson.tojson(userinfo); } catch (exception e) { system.out.println("exception: " + e.getmessage()); } return info; }
Comments
Post a Comment