从FTP服务器读取txt文件,并从AsyncTask的返回它 [英] Reading txt file from an ftp server and returning it from AsyncTask

查看:144
本文介绍了从FTP服务器读取txt文件,并从AsyncTask的返回它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 txt文件在我的 FTP服务器。和所有我想要做的就是读取文件,检索的内容,并通过使用的AsyncTask 保存为一个字符串。当我从logcat的理解,我能够连接 FTP服务器,我可以改变目录。但我不能阅读文本文件的情况下,我不知道如何从我的的AsyncTask 类返回。

而这些是我的codeS:

MainActivity.java

 包com.example.ftpdenemeleri;进口android.os.Bundle;
进口android.support.v7.app.ActionBarActivity;公共类MainActivity扩展ActionBarActivity {@覆盖
保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    FtpAsync任务1 =新FtpAsync();
    task1.execute();    }
}

FtpAsync.java

 包com.example.ftpdenemeleri;进口java.io.BufferedInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;进口org.apache.commons.net.ftp.FTPClient;
进口org.apache.commons.net.ftp.FTPCmd;
进口android.os.AsyncTask;公共类FtpAsync扩展的AsyncTask<太虚,太虚,字符串> {@覆盖
在preExecute保护无效(){
   super.on preExecute();
}@覆盖
保护字符串doInBackground(虚空...... PARAMS){
    // TODO自动生成方法存根    FTPClient ftpClient =新FTPClient();
    尝试{
        ftpClient.connect(主机名,21);        的System.out.println(ftpClient.getReplyString());
        ftpClient.sendCommand(FTPCmd.USER,用户);
        的System.out.println(ftpClient.getReplyString());
        ftpClient.sendCommand(FTPCmd.PASS,通);
        的System.out.println(ftpClient.getReplyString());
        ftpClient.sendCommand(FTPCmd.CWD,/家庭/网络/ bitirme);
        InputStream为=新的BufferedInputStream(ftpClient.retrieveFileStream(beaglesays.txt));
        的System.out.println(输入流已经打开。);      }赶上(IOException异常前){
        通信System.err.println(除息);
    }
    返回null;
}保护无效onPostExecute(){
        // TODO:检查this.exception
        // TODO:做一些配合饲料
    }
}


解决方案

有很多方法可以充分阅读的InputStream为一个字符串。一个最简单的是使用<一个href=\"http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html#toString%28java.io.InputStream,%20java.nio.charset.Charset%29\"相对=nofollow> IOUtils.toString() 从的 Apache的百科全书IO

因此​​,假设,这些变化将返回从表示字符串doInBackground() onPostExecute接受它()

I have a txt file in my ftp server. And all I want to do is reading that file, retrieve the contents and save it as a string by using AsyncTask. As I understand from the logcat, I'm able to connect ftp server and I can change directory. But I cannot read the context of the text file, and I don't know how to return it from the my AsyncTask class.

And these are my codes:

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
    }
}

解决方案

There are many ways to fully read an InputStream into a String. The easiest one would be to use IOUtils.toString() from Apache Commons IO.

So, assuming that, the changes would be to return said string from doInBackground() and receive it in onPostExecute().

这篇关于从FTP服务器读取txt文件,并从AsyncTask的返回它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆