Thread+handler+GET 网络操作

来源:互联网 发布:杭州网络推广平台 编辑:程序博客网 时间:2024/06/06 06:43

1、发送消息

    handler.sendEmptyMessage(2);

2、创建Handler

    private Handler handler = new Handler(Looper.myLooper()) {public void handleMessage(Message msg) {switch (msg.what) {case 11:shouGeRen((JSONObject) msg.obj);break;case 2:new Thread1().start();break;case 3:Xiugaigerenxinxi();break;default:break;}}};
3、Thread线程

    class Thread1 extends Thread {@Overridepublic void run() {gerenxinxi(uid);}}
4、实现类

    public void gerenxinxi(String userId) {url_constant1 = url1 + "uid=" + userId;try {HttpClient httpClient = new DefaultHttpClient();HttpGet httpGet = new HttpGet(url_constant1);HttpResponse res = httpClient.execute(httpGet);if (res.getStatusLine().getStatusCode() == 200) {HttpEntity httpEntity = res.getEntity();String json = EntityUtils.toString(httpEntity, "UTF-8");JSONObject jo = new JSONObject(json);JSONObject ja = jo.getJSONObject("data");Message message = new Message();message.what = 11;message.obj = ja;handler.sendMessage(message);}} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

0 0
原创粉丝点击