大家好,感谢您的阅读。 :)如何在等待HttpRespons时显示ProgressDialog?
我有这个Java(Android)代码正在发出HTTP请求并等待响应。该请求启动一个生成PDF文件并返回的服务。
该服务大约需要20秒,当用户等待时,我想显示进度对话框(不确定)。我试图在它自己的线程中显示对话框,这给我的运行时异常。我试着把请求和响应在自己的线程中,但没有等待负责完成,我得到一个空的PDF。
任何人都可以提出任何建议吗?这里的代码...
textContainer.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent getPdfFile = null;
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf");
if(!pdfFile.exists()) {
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getMethod = new HttpGet((
"http://myServices.mySite.org/services.ashx/PDFFILE?fileId="
+ fileId + "&account=" + accountId + "&dataset=" +
account.getProperty("DATASET").getValue().toString().trim()).replace(" ", "%20"));
HttpResponse response = client.execute(getMethod);
InputStream inStream = response.getEntity().getContent();
FileOutputStream fileWriter = new FileOutputStream(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf");
int dataByte = inStream.read();
while(dataByte > -1) {
fileWriter.write(dataByte);
dataByte = inStream.read();
}
fileWriter.close();
}
catch(Exception e) {
// TODO: Handle the exceptions...
}
getPdfIntent = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(pdfFile), "application/pdf");
startActivity(getPdfIntent);
}
});
非常感谢提前! :)
编辑:这里有一个例子,我已经使用AsyncTask来尝试解决问题。
textContainer.setOnClickListener(new View.OnClickListener() {
public void onClick(final View view) {
Intent getPdfFile = null;
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf");
if(!pdfFile.exists()) {
new AsyncTask<Boolean, Boolean, Boolean>() {
private ProgressDialog dialog;
protected void onPreExecute() {
dialog = ProgressDialog.show(view.getContext(), "Loading", "Please wait...");
}
protected Boolean doInBackground(Boolean... unused) {
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getMethod = new HttpGet((
"http://myServices.mySite.org/services.ashx/PDFFILE?fileId="
+ fileId + "&account=" + accountId + "&dataset=" +
account.getProperty("DATASET").getValue().toString().trim()).replace(" ", "%20"));
HttpResponse response = client.execute(getMethod);
InputStream inStream = response.getEntity().getContent();
FileOutputStream fileWriter = new FileOutputStream(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf");
int dataByte = inStream.read();
while(dataByte > -1) {
fileWriter.write(dataByte);
dataByte = inStream.read();
}
fileWriter.close();
}
catch(Exception e) {
// TODO: Handle the exceptions...
}
return true;
}
protected void onPostExecute(Boolean unused) {
dialog.dismiss();
}
}.execute(0);
getPdfIntent = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(pdfFile), "application/pdf");
startActivity(getPdfIntent);
}
});
但是,会发生什么是我不等待HttpRequest的响应,并继续作为如果回应immidiately返回。 : -/
您正在ui线程上进行网络调用。你不应该那样做。改为使用异步任务。 – njzk2 2012-03-07 09:38:00
njzk2是正确的,我认为它实际上是不允许在Android 3.0 SDK和以上 – Tony 2012-03-07 09:40:30
刚刚编辑我的帖子,包括我试过的AsyncTask,没有任何运气。 – 2012-03-07 14:37:09