0

我想开始一个从 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 中

4

3 回答 3

0

呃,我认为您不需要异步任务,而是需要在 ui 线程上运行

activity.runOnUiThread(new Runnable() {
       @Override
       public void run() {
              tvW.setText(responseString);
       }
});
于 2013-07-04T15:14:21.970 回答
0

You have to sync your Thread with the UI-Thread. Because all UI relevant operations like writing to an TextView have to be done on the UI-Thread. Therefor you should use the Handler class.

Handler myHandler = new Handler();

Now replace this code:

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

With this one:

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

Note that your Handler class have to be declared on the UI-Thread to get connected.

于 2013-07-04T15:51:33.770 回答
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 非常长

于 2013-07-05T09:05:54.903 回答