【发布时间】:2014-01-05 19:04:43
【问题描述】:
我想对从文本框中获取的 URL 进行简单的 HTTP 头请求。每次我输入 URL 并单击以获取 HTTP 响应时,应用程序都会变得无响应。这是代码:
public void MakeRequest(View v)
{
EditText mEdit;
TextView txtresponse;
txtresponse = (TextView)findViewById(R.id.textView1);
mEdit = (EditText)findViewById(R.id.editText1);
HttpClient httpClient = new DefaultHttpClient();
HttpHead httphead = new HttpHead(mEdit.getText().toString());
try {
HttpResponse response = httpClient.execute(httphead);
txtresponse.setText(response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
}
【问题讨论】:
-
当从 UI 线程做网络 IO 时,只要操作需要,App 就会冻结。完成后,应用程序应再次响应。考虑在另一个线程中执行阻塞操作,例如通过
AsyncTask。