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