【发布时间】:2012-03-07 09:35:12
【问题描述】:
大家好,感谢阅读。 :)
我有这个 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 的响应,而是继续,就好像响应立即返回一样。 :-/
【问题讨论】:
-
您正在 ui 线程上进行网络调用。你不应该那样做。改用异步任务。
-
njzk2 是正确的,我认为它实际上在 Android 3.0 SDK 及更高版本上是不允许的
-
刚刚编辑了我的帖子以包含我尝试过的 AsyncTask,但没有任何运气。
-
谁能告诉我我做错了什么?请...这是完成第一版的最后一件事。 :)
-
@RichB 在这一点上是正确的。您需要将您的
getPdfIntent = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(fakturaFil), "application/pdf"); startActivity(getPdfIntent);代码行移动到您的onPostExecute()中。现在,您正在创建任务、启动它,然后立即进入下一个活动。您需要等到任务完成。
标签: android pdf-generation httprequest httpresponse progressdialog