从ftp服务器读取文本文件并从AsyncTask返回

时间:2014-05-20 23:42:48

标签: android ftp android-asynctask return-value

我的txt file中有一个ftp server。我想要做的就是读取该文件,检索内容并使用AsyncTask将其保存为字符串。据我所知,从logcat,我能够连接ftp server,我可以更改目录。但我无法阅读文本文件的上下文,我不知道如何从我的AsyncTask类返回它。

这些是我的代码:

MainActivity.java

package com.example.ftpdenemeleri;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FtpAsync task1 = new FtpAsync();
    task1.execute();

    }
}

FtpAsync.java

package com.example.ftpdenemeleri;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPCmd;
import android.os.AsyncTask;

public class FtpAsync extends AsyncTask <Void, Void, String>{

@Override
protected void onPreExecute() {
   super.onPreExecute();      
}

@Override
protected String doInBackground(Void... params) {
    // TODO Auto-generated method stub

    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect("hostname", 21);

        System.out.println(ftpClient.getReplyString());
        ftpClient.sendCommand(FTPCmd.USER, "user");
        System.out.println(ftpClient.getReplyString());
        ftpClient.sendCommand(FTPCmd.PASS, "pass");
        System.out.println(ftpClient.getReplyString());
        ftpClient.sendCommand(FTPCmd.CWD, "/home/www/bitirme");
        InputStream is = new      BufferedInputStream(ftpClient.retrieveFileStream("beaglesays.txt")); 
        System.out.println("Input Stream has opened.");

      } catch (IOException ex) {
        System.err.println(ex);
    }
    return null; 
}

protected void onPostExecute() {
        // TODO: check this.exception 
        // TODO: do something with the feed
    }
}

3 个答案:

答案 0 :(得分:0)

尝试清楚地理解AsyncTask。 一些事情: (1)类似的问题:Howto do a simple ftp get file on Android (2)一旦你在doInBackground()中获得内容 - 将内容返回为String(目前你返回null)。 (3)在onPostExecute(String result)中添加参数,这将是文件的内容。

答案 1 :(得分:0)

有很多方法可以将InputStream完全读入String。最简单的方法是使用IOUtils.toString()中的Apache Commons IO

因此,假设这样做,更改将是从doInBackground()返回所述字符串并在onPostExecute()中接收。

答案 2 :(得分:0)

您的代码必须如下:

 package com.example.ftpdenemeleri;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPCmd;
import android.os.AsyncTask;

public class FtpAsync extends AsyncTask <Void, Void, String>{

@Override
protected void onPreExecute() {
super.onPreExecute();      
}

@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub

FTPClient ftpClient = new FTPClient();
try {
    ftpClient.connect("hostname", 21);

    System.out.println(ftpClient.getReplyString());
    ftpClient.sendCommand(FTPCmd.USER, "user");
    System.out.println(ftpClient.getReplyString());
    ftpClient.sendCommand(FTPCmd.PASS, "pass");
    System.out.println(ftpClient.getReplyString());
    ftpClient.sendCommand(FTPCmd.CWD, "/home/www/bitirme");
    InputStream is = new           BufferedInputStream(ftpClient.retrieveFileStream("beaglesays.txt")); 
    System.out.println("Input Stream has opened.");

Scanner s = new Scanner(is).useDelimiter("\\A"); // Convert your stream into string
        return s.hasNext() ? s.next() : ""; //send the string to onPostExecute()


  } catch (IOException ex) {
    System.err.println(ex);
}
return null; 
}


protected void onPostExecute(String result) {
    // TODO: check this.exception 
    // TODO: do something with the feed
Log.i("Result : ",result);
    }
}