android - How to get a JSON Object from a Jersey POST Response -


i working @ rest api new social network android app @ moment using spring android client , spring boot server.

i having trouble securing server using spring security, because don't understand how use properly. after reading tons of sample apps gave on spring security , found this tutorial on how secure apis using jersey.

instead of javascript using jersey client api test implementation

        client client = clientbuilder.newclient();     webtarget target = client.target("http://localhost:8080/demo-business-resource/login");      invocation.builder invobuilder = target.request();     invobuilder.header("service_key", "3b91cab8-926f-49b6-ba00-920bcf934c2a");        multivaluedmap formdata = new multivaluedmapimpl();       formdata.add("username", "username2");       formdata.add( "password", "passwordforuser2");       response response = invobuilder.post(entity.form(formdata));       system.out.println(response); response.getentity(); 

the post request successful, cant json object should entity of response. response.getentity(); returns httpurlconnector object.

here related server code

@post @path( "login" ) @produces( mediatype.application_json ) public response login(     @context httpheaders httpheaders,     @formparam( "username" ) string username,     @formparam( "password" ) string password ) {      demoauthenticator demoauthenticator = demoauthenticator.getinstance();     string servicekey = httpheaders.getheaderstring( demohttpheadernames.service_key );      try {         string authtoken = demoauthenticator.login( servicekey, username, password );          jsonobjectbuilder jsonobjbuilder = json.createobjectbuilder();         jsonobjbuilder.add( "auth_token", authtoken );         jsonobject jsonobj = jsonobjbuilder.build();          return getnocacheresponsebuilder( response.status.ok ).entity( jsonobj.tostring() ).build();      } catch ( final loginexception ex ) {         jsonobjectbuilder jsonobjbuilder = json.createobjectbuilder();         jsonobjbuilder.add( "message", "problem matching service key, username , password" );         jsonobject jsonobj = jsonobjbuilder.build();          return getnocacheresponsebuilder( response.status.unauthorized ).entity( jsonobj.tostring() ).build();     } }   private response.responsebuilder getnocacheresponsebuilder( response.status status ) {     cachecontrol cc = new cachecontrol();     cc.setnocache( true );     cc.setmaxage( -1 );     cc.setmustrevalidate( true );      return response.status( status ).cachecontrol( cc ); } 

i new jersey , want use generate authentication token, because 404 not found errors using spring. (i working spring-jersery ) here spring approach of client

        resttemplate template = new resttemplate();      multivaluemap<string, string> params = new linkedmultivaluemap<string, string>();     params.add("service_key", "3b91cab8-926f-49b6-ba00-920bcf934c2a");      multivaluemap<string,string> formdata=new linkedmultivaluemap<string,string>();     formdata.add("username", "username2");     formdata.add( "password", "passwordforuser2");     model_loginprofile log = new model_loginprofile();     log.setloginname("username2");     log.setpassword("passwordforuser2");       httpheaders requestheaders=new httpheaders();       requestheaders.setcontenttype(mediatype.application_json);       requestheaders.set("service_key", "3b91cab8-926f-49b6-ba00-920bcf934c2a");        httpentity<model_loginprofile> requestentity = new httpentity<model_loginprofile>(log, requestheaders);      responseentity<string> result = template.postforentity("http://localhost:8080/demo-business-resource/login", requestentity, string.class);     } 

maybe has fix well. :)

thanks in advance


Comments