Use handler and message to obtain the data returned from the server by the xutils POST request.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.