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