Android Handler pass data

Source: Internet
Author: User

Cause: A thread needs to be re-opened when Android uses a GET request to get a verification code, which results in my inability to get the data returned from the server backend

Workaround: Create a global variable, return the returned data to handler, and then assign the data to the global variable in handler

Some of the code is as follows:

Privatevoid Getyzm (IntMoney) {//TODO auto-generated Method Stub String res = ""; Date Nowtime =NewDate (); SimpleDateFormat time =New SimpleDateFormat ("YYMMDDHHMM"); String Tel =Phoneutils.getphonenumber (context);Final String paramdata = "imsi=" + imsi + "&imei=" + IMEI + "&tel=" + Tel + "&money=" + Money + "&app=" + app+ "&time=" +Time.format (Nowtime); String ResultCode;NewThread () {PublicvoidRun () {Final String Gaincode =Httprequest.sendget (URL, paramdata);//URL is the request verification code address, is a global variable, not listed here, Paramdata is the request parameter, HttpRequest is a tool class, the following send a linkString ResultCode; Jsonobject Datajson; String OrderID;Try{Datajson =NewJsonobject (Gaincode); ResultCode = datajson.getstring ("ResultCode"); Orderid=datajson.getstring ("OrderID");if ("200000". Equals (ResultCode)) {msg =Handler.obtainmessage (); Bundle bundle =NewBundle (); Bundle.putstring ("OrderID", OrderID);//Upload the order number returned by the server to the bundle, and then pass the handler outMsg.setdata (bundle); MSG.ARG1 = 0;//0 success for obtaining verification codeHandler.sendmessage (msg); }Else{msg =Handler.obtainmessage (); MSG.ARG1 = 1;//-1 failed to get verification codeHandler.sendmessage (msg); } }Catch(Jsonexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }}}.start (); }Class MsghandlerExtendsHandler {PrivateActivity activity;PublicMsghandler (activity activity) {this.activity=Activity } @OverridePublicvoidHandlemessage (Message msg) {//TODO auto-generated Method StubSwitch(MSG.ARG1) {Case 0: Dialog.show (); Bundle bundle=Msg.getdata (); Orderid=bundle.getstring ("OrderID");//The OrderID here is a global variableBreakcase 1 Showinfo (" Failed to get verification code!) "); breakcase 2: Showinfo ("Order successful!) "); breakcase 3 Showinfo (" Submit a CAPTCHA error, please re-test!) "); breakdefaultbreak; } super.handlemessage (msg);}      

Android Handler pass data

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.