大家好,感谢阅读。 :)
我有这个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的响应并继续,好像响应是立即返回的。 : - /
答案 0 :(得分:2)
看起来您需要将最后两行放在else {}
块内以及onPostExecute
中,以便只有在文件存在或AsyncTask完成后才会执行。
答案 1 :(得分:0)
您需要使用AsyncTask ...本教程应该为您提供基础知识。
答案 2 :(得分:0)
你应该使用AsyncTask来做到这一点。在覆盖方法“doInBackground”上你可以在覆盖方法“onPreExecute”上发布你的httppost和showdialog,然后在“onPostExecute”上取消它