0

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
    }
}
Onik
  • 19,396
  • 14
  • 68
  • 91
Ceyhan ILERI
  • 57
  • 2
  • 17

3 Answers3

1

Your code must be like :

 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);
    }
}
akh
  • 23
  • 7
  • Hi @AosKhalifa, when i try to use scanner like your suggestion, rest of the code become unreachable. I've tested it by using System.out.println and i got "unreachable code" warning. – Ceyhan ILERI May 21 '14 at 00:29
0

Try to understand AsyncTask clearly. Few things: (1) Similar question: Howto do a simple ftp get file on Android (2) Once you get content in doInBackground() - return the content as String(at present you are returning null). (3) Add parameter in onPostExecute(String result) and this will the content of the file.

Community
  • 1
  • 1
araut
  • 576
  • 4
  • 11
  • I've seen that question, but since i'm kinda new to java and android, i didn't know what to do with InputStream and InputStreamReader, that's the point that i didn't understand. About AsyncTask, you are right, i still can't get how to pass string to onPostExecute from doInBackground. – Ceyhan ILERI May 21 '14 at 02:24
0

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().

matiash
  • 54,791
  • 16
  • 125
  • 154