大家好,感謝您的閱讀。 :)如何在等待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