我在活动中使用ftphelper,async如何在活动中使用getreplycode()?

smdncfj3  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(235)

我有一个活动,它创建了一个asynctask来通过ftphelper类测试ftp连接。活动将显示一个toast,其中包含连接的布尔状态,即success或fail。如何在活动中显示确切的replycode或replystring?
活动:

  1. TestConnection task = new TestConnection(NewSite.this,
  2. _address, _user, _pass, p, new testConnInterface() {
  3. @Override
  4. public void testConnection(boolean result) {
  5. if (result == true) {
  6. Toast.makeText(NewSite.this,
  7. "Connection Succesful",
  8. Toast.LENGTH_LONG).show();
  9. } else {
  10. Toast.makeText(NewSite.this,
  11. "Connection Failed:" + result,
  12. Toast.LENGTH_LONG).show();
  13. }
  14. }
  15. });
  16. task.execute();

测试连接.java

  1. public class TestConnection extends AsyncTask<Void, Void, Boolean> {
  2. private Context mContext;
  3. private testConnInterface mListener;
  4. private FTPHelper ftpHelper = new FTPHelper();
  5. private String _address;
  6. private String _user;
  7. private String _pass;
  8. private int _port;
  9. ProgressDialog progressDialog;
  10. public interface testConnInterface {
  11. public void testConnection(boolean result);
  12. }
  13. public TestConnection(Context context, String address, String user,
  14. String pass, int port, testConnInterface mListener) {
  15. mContext = context;
  16. _address = address;
  17. _user = user;
  18. _pass = pass;
  19. _port = port;
  20. this.mListener = mListener;
  21. }
  22. // declare other objects as per your need
  23. @Override
  24. protected void onPreExecute() {
  25. progressDialog = ProgressDialog.show(mContext, "Please wait",
  26. "Attempting to connect", true);
  27. // do initialization of required objects objects here
  28. };
  29. @Override
  30. protected Boolean doInBackground(Void... params) {
  31. boolean status = ftpHelper.ftpConnect(_address, _user, _pass, _port);
  32. return status;
  33. }
  34. @Override
  35. protected void onPostExecute(Boolean result) {
  36. super.onPostExecute(result);
  37. if (mListener != null)
  38. mListener.testConnection(result);
  39. progressDialog.dismiss();
  40. };
  41. }

ftphelper.java文件

  1. public class FTPHelper {
  2. public static FTPClient mFTPClient = null;
  3. public FTPHelper() {
  4. // TODO Auto-generated constructor stub
  5. }
  6. public boolean ftpConnect(String host, String username, String password,
  7. int port) {
  8. try {
  9. mFTPClient = new FTPClient();
  10. // connecting to the host
  11. mFTPClient.connect(host, port);
  12. // now check the reply code, if positive mean connection success
  13. if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
  14. // login using username & password
  15. boolean status = mFTPClient.login(username, password);
  16. /*
  17. * Set File Transfer Mode
  18. *
  19. * To avoid corruption issue you must specified a correct
  20. * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
  21. * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE for
  22. * transferring text, image, and compressed files.
  23. */
  24. mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
  25. mFTPClient.enterLocalPassiveMode();
  26. showServerReply(mFTPClient);
  27. return status;
  28. }
  29. } catch (Exception e) {
  30. // Log.d(TAG, "Error: could not connect to host " + host );
  31. }
  32. return false;
  33. }
  34. public boolean ftpDisconnect() {
  35. try {
  36. mFTPClient.logout();
  37. mFTPClient.disconnect();
  38. return true;
  39. } catch (Exception e) {
  40. // Log.d(TAG,
  41. // "Error occurred while disconnecting from ftp server.");
  42. }
  43. return false;
  44. }
  45. public String ftpGetCurrentWorkingDirectory() {
  46. try {
  47. String workingDir = mFTPClient.printWorkingDirectory();
  48. return workingDir;
  49. } catch (Exception e) {
  50. Log.i("Error working dir", e.toString());
  51. }
  52. return null;
  53. }

基本上我想知道如何将getreplycode()返回到我的活动中。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题