以前にASyncTaskを使用したことがあり、問題は発生していません。このコードが機能しない理由がわかりません。TextViewに設定したテキストファイルをダウンロードするには、ftpサーバーにアクセスする必要があります。ASyncTask内のコードは正常に機能します。確認しました。何らかの理由で、新しいスレッド'new GetStory()。execute'を呼び出すと問題が発生します。どんな助けでも素晴らしいでしょう。
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();
}
}
}