private String getTestContent(String url) {
        HttpGet httpRequest = new HttpGet(url);
        String result = null;
        BufferedReader br = null;
        InputStream is = null;
        try {
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);            
            int response = httpResponse.getStatusLine().getStatusCode();
            if (response == 200) {
                HttpEntity entity = httpResponse.getEntity();
                StringBuffer sb = new StringBuffer();
                is = entity.getContent();
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String data = "";
                boolean found = false;
                while ((data = br.readLine()) != null) {
                    if(data != null && data.indexOf(TEST_CONTENT_START_TAG) != -1) {
                        found = true;
                    }
                    else {
                        if (found) {
                            if ((data!=null) && (data.indexOf(TEST_CONTENT_END_TAG)!=-1)) {
                                found = false;
                                break;
                            }
                            sb.append(data);
                            sb.append("\n");
                        }
                    }
                }
                
                result = sb.toString();
                return result;
            } else {
                return result;
            }
        } catch (ClientProtocolException e) {
            return null;
        } catch (HttpHostConnectException e) {
            return null;
        } catch (UnknownHostException e) {
            return null;
        } catch (IOException e) {
            return null;
        } catch (Exception e) {
            return null;
        } finally {
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            if(br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

解决方案 »

  1.   

    当你在UI线程中new一个线程出来之后,这里面的操作就不在UI线程中了,如果你要改变界面等内容,就必须要到UI线程中完成,所以就需要使用到handler
      

  2.   

    当你的线程需要操作UI的时候(例如改变个textview的值之类的)需要发消息给主线程让主线程来完成这个操作,handle就是负责发送消息的!
    因为线程直接操作UI不安全,现在的语言都不支持在线程中操作UI!
      

  3.   

    我想问下不如Handler.post一个runable?
      

  4.   

    因为Android规定非UI线程不能刷新界面的
      

  5.   

    很简单,因为 UI 的操作是垄断的,必须拜求 Handler 一伙,别生气!