Note: Data returned from the server should be processed in handlemessage. Otherwise, the results will not be obtained due to thread issues.
Public class mainactivity extends activity {private string responseinfo; private handler; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); handler = new handler () {@ override public void handlemessage (Message MSG) {super. handlemessage (MSG); responseinfo = (string) MSG. OBJ; gson = new gson (); Type Type = new typetoken <jsonbean> (){}. getType (); jsonbean = gson. fromjson (responseinfo, type); system. out. println (jsonbean) ;}}; getfromserver () ;}/ *** use xutils to send a POST request to get the data returned by the server */Public void getfromserver () {string url = "http: // 182.92.195.162: 8088/index. PHP? R = API/client/init "; requestparams Params = new requestparams (); jsonobject JSON = new jsonobject (); jsonobject request = new jsonobject (); try {JSON. put ("uid", ""); JSON. put ("Sid", ""); JSON. put ("Ver", "1"); JSON. put ("request", request); Params. addbodyparameter ("JSON", JSON. tostring ();} catch (exception e) {e. printstacktrace ();} httputils HTTP = new httputils (); http. send (httpmethod. post, URL, Params, new requestcallback <string> () {@ override public void onfailure (httpexception arg0, string arg1) {}@ override public void onsuccess (responseinfo <string> arg0) {message MSG = message. obtain (); MSG. OBJ = arg0.result; handler. sendmessage (MSG );}});}}
Use handler and message to obtain the data returned from the server by the xutils POST request.