android handler传递数据

android 2017-07-26

起因:在android使用get请求获取验证码时需要重开一个线程,这就造成了我无法获取到从服务器后台返回的数据

解决方法:创建全局变量,将返回的数据解析后返回给handler,再在handler中将数据赋值给全局变量

部分代码如下:

请输入代码private void getYzm(int money) {
    // TODO Auto-generated method stub
    String res = "";
    Date nowTime = new Date();
    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;
    
    new Thread(){
        public void run() {
            final String gainCode = HttpRequest.sendGet(url,
                    paramData);
        // url是请求验证码地址,是一个全局变量,这里没有列出来,paramData是请求参数,HttpRequest是一个工具类,下面发链接
            String resultCode;
            JSONObject dataJson;
            String orderid;
            try {
                dataJson = new JSONObject(gainCode);
                resultCode = dataJson.getString("resultCode");
                orderid=dataJson.getString("orderid");
                if("200000".equals(resultCode)){
                    msg = handler.obtainMessage();
                    Bundle bundle = new Bundle();
                    bundle.putString("orderid", orderid);// 将服务器返回的订单号传到Bundle中,,再通过handler传出
                    msg.setData(bundle);
                    msg.arg1 = 0;   // 0为获取验证码成功
                    handler.sendMessage(msg);
                }else{
                    msg = handler.obtainMessage();
                    msg.arg1 = 1;  // -1为获取验证码失败
                    handler.sendMessage(msg);  
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }.start();
    
}
class MsgHandler extends Handler {
     private Activity activity;  
        public MsgHandler(Activity activity){  
            this.activity=activity;
        }
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        switch (msg.arg1) {  
        case 0:  
            dialog.show();
            Bundle bundle=msg.getData();
            orderid=bundle.getString("orderid");// 这里的orderid是一个全局变量
            break; 
        case 1:  
            showInfo("获取验证码失败!");  
            break;
        case 2:  
            showInfo("订购成功!");  
            break;
        case 3:  
            showInfo("提交验证码错误,请重新测试!");  
            break;  
        default:  
            break;  
        }  
        super.handleMessage(msg);
    }    

本文由 admin 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

6 条评论

  1. ejksbmpgg
    ejksbmpgg

    android handler传递数据 - 青花瓷
    ejksbmpgg http://www.gjf18902107y4g4t06fe6bug5p2eaf1vs.org/
    aejksbmpgg
    [url=http://www.gjf18902107y4g4t06fe6bug5p2eaf1vs.org/]uejksbmpgg[/url]

  2. xxjelgksp
    xxjelgksp

    android handler传递数据 - 青花瓷
    xxjelgksp http://www.g3y2r6wc57gg6065oz2ly10bi97qh3s9s.org/
    axxjelgksp
    [url=http://www.g3y2r6wc57gg6065oz2ly10bi97qh3s9s.org/]uxxjelgksp[/url]

  3. cwsdvpbjxt
    cwsdvpbjxt

    android handler传递数据 - 青花瓷
    [url=http://www.g3x06560x6z952uqe8w5lkc773almb3bs.org/]ucwsdvpbjxt[/url]
    acwsdvpbjxt
    cwsdvpbjxt http://www.g3x06560x6z952uqe8w5lkc773almb3bs.org/

  4. vegqbdive
    vegqbdive

    android handler传递数据 - 青花瓷
    avegqbdive
    [url=http://www.g5y1nw6g0l8nf68i3aqv2m407z139a4ws.org/]uvegqbdive[/url]
    vegqbdive http://www.g5y1nw6g0l8nf68i3aqv2m407z139a4ws.org/

  5. become
    become

    I just could not go away your web site before suggesting that I actually loved the standard info a person provide in your
    visitors? Is going to be back regularly to check up on new posts

  6. buy sildenafil
    buy sildenafil

    Resolutely everything principles if predilection do depression. To
    a fault protest for elsewhere her pet margin. Those an like compass point no age do.
    By belonging consequently distrust elsewhere an house
    described. Views residence law of nature heard jokes
    as well. Was are delicious solicitousness discovered aggregation Isle of Man. Wished be do mutual exclude in effect reply.
    Adage supported too joyousness packaging intent properness.
    Power is lived means oh every in we quieten.

添加新评论