handler中传递JSON数据的写法

package com.zq.jsontest;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
//  private JSONArray jsonArray;

    private Handler handler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            switch(msg.what)
            {
               case 0:
                   try {
                    JSONObject data1 = (JSONObject) msg.obj;
                    String id = data1.getString("id");
                    String name = data1.getString("name");
                    String version = data1.getString("version");
                    tv1.setText("id is: " + id);
                    tv2.setText("name is: " + name);
                    tv3.setText("version is: " + version);
                   } catch (JSONException e) {
                    // TODO Auto-generated catch block
                      e.printStackTrace();
                   } 
                   break;
               default:
                   break;
            }
        }

    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendRequestWithHttpClient();
        init();
    }

    private void init() {
        // TODO Auto-generated method stub
        tv1 = (TextView)findViewById(R.id.num);
        tv2 = (TextView)findViewById(R.id.name);
        tv3 = (TextView)findViewById(R.id.version);
    }

    private void sendRequestWithHttpClient() {
        // TODO Auto-generated method stub
        new Thread(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub

                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet("http://10.0.6.13/test/data.json");
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    if(httpResponse.getStatusLine().getStatusCode() == 200)
                    {
                        HttpEntity entity = httpResponse.getEntity();
                        String response = EntityUtils.toString(entity, "utf-8");

                        parseJSONWithJSONObject(response);

                    }

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            }).start();
    }

    private void parseJSONWithJSONObject(String jsonData) {
        // TODO Auto-generated method stub

/*          for(int i=0;i<jsonArray.length();i++)
            {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String num = jsonObject.getString("id");
                String name = jsonObject.getString("name");
                String version = jsonObject.getString("version");
                Log.d("ZQ", "id is" + num);
                Log.d("ZQ","name is" + name);
                Log.d("ZQ", "version is" + version);
            }
*/
            try {
                //创建JSON对象
                JSONObject data = new JSONObject(jsonData);
                Message message = new Message();
                message.what = 0;
                message.obj = data;
                handler.sendMessage(message);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值