【发布时间】:2014-12-16 12:47:48
【问题描述】:
我使用了 asyncTask 来发出 http 请求,因此我使用了进度条来指示该线程中正在进行的进度。在调用onPostexecute() 方法之前一切正常,我在Logcat 中显示了响应字符串,但我的进度条没有退出。
有人问过类似的问题,但我没有得到该问题所需的解决方案。 请帮忙。
这是我的代码:
class RequestTask extends AsyncTask<String, Integer, String> {
@SuppressWarnings("static-access")
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
prgDialog=new ProgressDialog(getActivity());
prgDialog.setCancelable(true);
prgDialog.show(getActivity(), null, "Loading...");
}
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else {
// Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
// TODO Handle problems..
} catch (IOException e) {
// TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("result", result);
prgDialog.dismiss();
// Do anything with response..
}
}
【问题讨论】:
-
您是否尝试将
prgDialog.dismiss();放在super.onPostExecute()之前? -
是的,我做到了。但它没有用。
-
你确定你的 onPostExecute() 方法被调用了吗?
-
@joao2fast4u 是的,我可以看到我在 Logcat 上收到的响应,我在 postExecute 方法中包含了代码。
标签: android android-asynctask progress-bar