java - what does this code do, I need to understand -
if can explain these lines of codes does/mean. grateful. thanks
jsonobject req = new jsonobject(); boolean flag = false; seek { req.put("name", p_name.gettext().tostring()); string res = httpclient.sendhttppost(constants.name, req.tostring()); if(res != null){ jsonobject json = new jsonobject(res); if(json.getboolean("status")){ flag = true; string id = json.getstring("userid"); app.getuserinfo().setuserinfo(id); } }
in brief
this code sends name remote api, returns userid , successful status (presumably if name found remote service). userid stored in our local application.
line line explanation
first, create json object named req.
jsonobject req = new jsonobject(); then save string stored in p_name name field of req
boolean flag = false; seek { req.put("name", p_name.gettext().tostring()); then http post string serialization of json object our server. res store response receive string.
string res = httpclient.sendhttppost(constants.name, req.tostring()); after post returns, check response see if it's null.
if(res != null){ if it's not null, turn response json object (presumably server returns valid json.
jsonobject json = new jsonobject(res); we check see if field status in our response object true. (response {"status":"true","userid":"a-user-id"} if looked @ raw server output.)
if(json.getboolean("status")){ if so, set flag true, field userid response object, , set our application's user id returned id server.
flag = true; string id = json.getstring("userid"); app.getuserinfo().setuserinfo(id); java android
No comments:
Post a Comment