在Thread内部启动AsyncTask

时间:2013-07-04 14:51:37

标签: java android android-asynctask

我想开始一个简单的过程,从AsyncTask中获取网页的HTML代码,因为当它来自新的线程时,UI冻结了一会儿,我该怎么办呢?

public void loadHTML(View vL) {
    final String ss = (et.getText().toString());

    new Thread(new Runnable() {

        @Override
        public void run() {

            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(new HttpGet(ss));
                StatusLine statusLine = response.getStatusLine();
                if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    response.getEntity().writeTo(out);
                    out.close();
                    final String responseString = out.toString();
                    tvW.post(new Runnable() {
                        @Override
                        public void run() {
                            tvW.setText(responseString);
                        }
                    });


                    Log.v("DYRA BYRA", responseString);
                } else {
                    response.getEntity().getContent().close();
                }
            } catch (Exception e) {
            }
        }
    }).start();

}

我想要这部分tvW.post(new Runnable() { @Override public void run() { tvW.setText(responseString); } });

在AsyncTask中

3 个答案:

答案 0 :(得分:0)

呃我认为你不需要asynctask,而是需要在ui线程上运行

activity.runOnUiThread(new Runnable() {
       @Override
       public void run() {
              tvW.setText(responseString);
       }
});

答案 1 :(得分:0)

您必须将您的Thread与UI-Thread同步。因为所有与UI相关的操作(如写入TextView)都必须在UI-Thread上完成。因此,您应该使用Handler类。

Handler myHandler = new Handler();

现在替换此代码:

tvW.post(new Runnable() {
     @Override
     public void run() {
          tvW.setText(responseString);
     }
});

有了这个:

 myHandler.post(new Runnable() {
     @Override
     public void run() {
          tvW.setText(responseString);
     }
});

请注意,必须在UI-Thread上声明您的Handler类才能连接。

答案 2 :(得分:0)

这就是我想做的事情

public void loadHTML(View vL) {
    new getCode().execute();
}

private class getCode extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        final String ss = (et.getText().toString());
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(new HttpGet(ss));
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
                Log.v("DYRA BYRA", responseString);
            } else {
                response.getEntity().getContent().close();
            }
        } catch (Exception e) {

        }
        return null;

    }

    protected void onPostExecute(String result) {
        tvW.setText(responseString);
    }

}
UI仍然会冻结半秒钟,但也许这是因为收到的HTML非常长