-1

My goal is to to convert a .txt file on an FTP page to a simple String for easy manipulation.

The specific .txt file is here: ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt. It is an anonymous FTP page, so when I use my computer's browser, there's no need for a username or password.

I've tried incorporating different codes and tips from the following sources:

None of what I've tried above helped. I'm not quite sure what I'm doing wrong.

To be clear, all I want to do is to get the plain text from the posted .txt file. I have no interest in downloading said file onto my device's memory.

If you could provide me with a step-by-step explanation on how to do this, I'd be very thankful.

Community
  • 1
  • 1
user2323030
  • 1,193
  • 4
  • 16
  • 37
  • 2
    *I'm not quite sure what I'm doing wrong.* Me too, because **you did not provide the code that you had tried** .. *step-by-step explanation on how to do this* you mean: write the code for you? – Selvin Jan 21 '15 at 13:14
  • I already wrote in the question that I already incorporated code from the different sources. They're too varied to post on here, the question would be pages and pages long. I'm looking for something more unified from someone who knows better. – user2323030 Jan 21 '15 at 13:16
  • incorporated means copy&paste? then maybe you should change the server address ... seriously, lets take a look at the first link ... the code should works with only few (obvious) fixes .. first URL should be `ftp://example.com/thefile.txt` (starts with `ftp` scheme)... and of course you would get NetworkOnMainThreadException (but it is not a problem since the question about this exception was asked many times here) – Selvin Jan 21 '15 at 13:21
  • Alright... Let's tone down the attitude here. If you can't help, please move on. There's no need to troll someone who doesn't know how to do something you consider simple. – user2323030 Jan 21 '15 at 13:22
  • 1
    you are right, there is no need to troll stack overflow with such questions ... please read http://stackoverflow.com/tour ... you should rather ask something like : i did tried code from [link], my code lnow looks like [your code], now when i tried to run it on device/emulator, i'm getting such error/exception ... – Selvin Jan 21 '15 at 13:28
  • Well... Thanks for the edit to your last comment. At least it's more constructive now. I appreciate it. – user2323030 Jan 23 '15 at 14:22

2 Answers2

3

Ok. I've got it. For those who are in the same boat, the step-by-step answer is below:

  1. A lot of problems other users were encountering could be solved by having permissions for internet turned on in the manifest, but mine was a little more complicated. Turns out, the main trick is to not include ftp:// in the address in Java.* Also, when you are entering an FTP site, make sure you enter via the root page, so my original page of ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt becomes: ftp.nasdaqtrader.com.

  2. Make sure to download and include the right Apache Commons library in your project (here: http://commons.apache.org/proper/commons-net/download_net.cgi).

  3. Connect to the root page:

    FTPClient ftpClient = new FTPClient();

    ftpClient.connect("ftp.nasdaqtrader.com");

  4. Login anonymously, in this case, both username and password are "anonymous". This might be the case with many FTP pages, but I can't be sure:

    ftpClient.login("anonymous", "anonymous");

  5. Then, go to the correct directory (be careful about including/excluding the slashes):

    ftpClient.changeWorkingDirectory("/SymbolDirectory");

  6. Finally! You can now get the InputStream from the posted text file:

    InputStream is = new BufferedInputStream(ftpClient.retrieveFileStream("nasdaqlisted.txt"));

  7. Then, convert the InputStream into String and manipulate as needed. There are many ways to do this. One of which can be found here: How can I convert InputStream data to String in Android SOAP Webservices

*Source: Android FTP connection Failed

Community
  • 1
  • 1
user2323030
  • 1,193
  • 4
  • 16
  • 37
0

If you get "425 Unable to build data connection: Connection timed out" error, then after connecting to ftp server, I would recommend you to set the local mode to passive mode by the following statement.

ftpClient.enterLocalPassiveMode();
Jose Ricardo Bustos M.
  • 8,016
  • 6
  • 40
  • 62
burcak
  • 1,009
  • 10
  • 34