我正在学习android开发,我需要检查一个文件是否存在于服务器上。
我使用了以下代码
public class CheckReportExists extends AsyncTask<String, Void, Boolean>{
Boolean fileExists = false;
public CheckReportExists() {
}
public Boolean CheckReportExists1(String download_url){
execute(download_url);
return fileExists;
}
@Override
protected void onPreExecute() {
//display progress dialog.
}
@Override
protected Boolean doInBackground(String... params) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(params[0]).openConnection();
con.setRequestMethod("HEAD");
int response = con.getResponseCode();
if(response == HttpURLConnection.HTTP_OK){
fileExists = true;
}
} catch(Exception e){
}
return fileExists;
}
@Override
protected void onPostExecute(Boolean result) {
// dismiss progress dialog and update ui
//super.onPostExecute(result);
}
}
我使用以下代码来调用它
CheckReportExists cre = new CheckReportExists();
Boolean fileExists = cre.CheckReportExists1(download_url);
if(fileExists) {
builder.setPositiveButton("Download Report", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new DownloadTask(Results.this, download_url);
}
});
}else{
builder.setPositiveButton("Report not ready yet", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
但这段代码在我得到 AlertDialog
与 "报告尚未准备好 "按钮,即使文件存在于服务器上。
谢谢你。
类似这样的东西。
CheckReportExists cre = new CheckReportExists() {
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
// RIGHT HERE is where you can add your code to handle the result
}
};
// CheckReportExists1 is no longer a good name because it's not
// returning a value. It just starts the process.
cre.CheckReportExists1(download_url);
CheckReportExists1()
总是会返回一个 false
值;这是因为它在UI线程中运行,不会等到 AsyncTask
完成其后台工作。所以它总是返回它的初始分配值,即 false
为了解决这个问题,你可以创建一个带有回调方法的监听器接口,这个回调方法需要一个 Boolean
表示该文件是否存在。并在后台工作结束后,每当你真正确定文件是否存在时,就触发这个回调。
所以用以下方法修改你的代码。
监听器接口:
interface OutputListener {
void checkFile(Boolean exists);
}
AsyncTask:
public class CheckReportExists extends AsyncTask<String, Void, Boolean>{
Boolean fileExists = false;
OutputListener listener;
public CheckReportExists() {
}
public void CheckReportExists1(String download_url, OutputListener outputListener){
execute(download_url);
listener = outputListener;
}
@Override
protected void onPreExecute() {
//display progress dialog.
}
@Override
protected Boolean doInBackground(String... params) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(params[0]).openConnection();
con.setRequestMethod("HEAD");
int response = con.getResponseCode();
if(response == HttpURLConnection.HTTP_OK){
fileExists = true;
}
} catch(Exception e){
}
return fileExists;
}
@Override
protected void onPostExecute(Boolean result) {
if (listener != null)
listener.checkFile(result);
// dismiss progress dialog and update ui
//super.onPostExecute(result);
}
}
AsyncTask的用法:
CheckReportExists cre = new CheckReportExists();
cre.CheckReportExists1(download_url, new OutputListener() {
@Override
public void checkFile(Boolean exists) {
if(fileExists) {
builder.setPositiveButton("Download Report", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new DownloadTask(Results.this, download_url);
}
});
} else {
builder.setPositiveButton("Report not ready yet", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
}
});