我之前使用过ASyncTask并且没有遇到任何问题。我不明白为什么这段代码不起作用。我必须访问ftp服务器才能下载我设置为TextView的文本文件。 ASyncTask中的代码工作正常。我检查了一下。出于某种原因,当我调用新线程' new GetStory()时,我遇到了问题。执行'。任何帮助都会很棒。
package com.amazingstories;
import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
public class DisplayStory extends Activity {
String path;
TextView story;
FTPClient ftp = new FTPClient();
File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
path = getIntent().getExtras().getString("path");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.displaystory);
story = (TextView) findViewById(R.id.tvStory);
story.setText(path);
file = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"temp.txt");
new GetStory().execute();
// try{
// new GetStory().execute();
// }catch(Exception e){
// Toast.makeText(getApplicationContext(), e.toString(),
// Toast.LENGTH_SHORT).show();
//
// }
}
public class GetStory extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Try",
Toast.LENGTH_SHORT).show();
try {
Toast.makeText(getApplicationContext(), "Try",
Toast.LENGTH_SHORT).show();
if (!ftp.isConnected()) {
ftp.connect(".....");
Toast.makeText(getApplicationContext(),
"connected to server", Toast.LENGTH_SHORT).show();
ftp.login("....", ".....");
}
Toast.makeText(getApplicationContext(), "FTP Connected",
Toast.LENGTH_SHORT).show();
ftp.changeDirectoryUp();
ftp.download(path, file, new FTPDataTransferListener() {
//ProgressDialog dialog;
@Override
public void aborted() {
// TODO Auto-generated method stub
// dialog.dismiss();
story.setText("Transfer Aborted");
}
@Override
public void completed() {
// TODO Auto-generated method stub
//dialog.dismiss();
}
@Override
public void failed() {
// TODO Auto-generated method stub
//dialog.dismiss();
story.setText("Transfer Failed");
}
@Override
public void started() {
// TODO Auto-generated method stub
//dialog = ProgressDialog.show(DisplayStory.this, "",
// "Loading. Please wait...", true);
}
@Override
public void transferred(int arg0) {
// TODO Auto-generated method stub
}
});
// tv.setText(test.toString());
Toast.makeText(getApplicationContext(), "Done",
Toast.LENGTH_SHORT).show();
} catch (FTPException e) {
Toast.makeText(getApplicationContext(),
"Exception Caught " + e, Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),
"Exception Caught " + e, Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),
"Exception Caught " + e, Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),
"Exception Caught " + e, Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (FTPDataTransferException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),
"Exception Caught " + e, Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (FTPAbortedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
ftp.logout();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FTPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
getFileData get = new getFileData();
try {
story.setText(get.getData(file));
Toast.makeText(getApplicationContext(), "Set TextView",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
file.delete();
}
}
}
答案 0 :(得分:1)
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Try",
Toast.LENGTH_SHORT).show();
您不能在doInBackground内调用Toast。 doInBackground方法在非ui线程中运行,因此您无法在UI上操作或sidplay Toast。它必须在onPreExecute或onPostExceute中完成。
答案 1 :(得分:0)
方法onPreExecute()
,onPostExecute()
和onProgressUpdate()
是AsyncTask
执行期间在主(UI)线程上调用的唯一方法,因此是只有您可以直接调用UI方法(例如TextView.setText()
和Toast.makeText()
)。
您的程序可能会崩溃,因为您无法从“未准备好循环器”的线程创建Toast,这是从doInBackground()
执行此操作时发生的事情;这会导致异常和崩溃。