2

I am connected to a Server whose address is 192.168.1.254 and on typing this address in the browser it is showing the list of available folder enter image description here i want to display the name of the folder in my android application i have tried the following code but no-luck.

try {
        SmbFile test = new SmbFile("smb://192.168.1.254");
        SmbFile[] files = test.listFiles();
        if (files != null)
            for (SmbFile s : files) {
                Log.d("debug", s.getName());
            }
    } catch (SmbException e) {
        Log.d("debug", "ERROR");
    } catch (Exception e) {
        Log.d("debug", "ERROR");
    }

which i find here and i also tried

File f = new File("//192.168.1.254");
//also tried with File f = new File("http//192.168.1.254");
File[] files = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
    // Specify the extentions of files to be included.
    return name.endsWith(".bmp") || name.endsWith(".gif");
}
});

 // get names of the files
String[] fileNamesArray = null; 
for (int indx = 0; indx < files.length(); indx++) {
Log.d("name",files[indx].getName());
}

return fileNamesArray; 
Shubham Jain
  • 2,365
  • 2
  • 17
  • 29

2 Answers2

0

Seems you need FTP client for list files on remote location. Try to use, for example, ftp4j library as described in it's official documentation or this Android Example by devert. Exactly list files on remote FTP server with ftp4j by Arun you can find here:

    FTPClient client = null;
    try {       // Get the FTP Connection from the
        Utility class client =FTPUtility.connect(ipAddress, userName,
                password);
        if (client != null) {           /* List all file inside the directory */
            FTPFile[] fileArray = client.list();
            System.out.println("List of files...");
            for (int i = 0; i < fileArray.length; i++) {
                FTPFile file = fileArray[i];
                if (file != null) {
                    if (file.TYPE_FILE == FTPFile.TYPE_FILE) // File                    {
                        System.out.println("File Name = " + file.getName() + " ; File Size = " + file.getSize() + " ;Modified Date = " + file.getModifiedDate());
                } else if (file.TYPE_DIRECTORY == FTPFile.TYPE_DIRECTORY) // Directory
                {
                    System.out.println("Directory Name = " + file.getName() + " ; Directory Size = " + file.getSize() + " ;Modified Date = " + file.getModifiedDate());
                } else if (file.TYPE_LINK == FTPFile.TYPE_LINK) // Link
                {
                    System.out.println("Link Name = " + file.getName() + " ;Modified Date = "
                            + file.getModifiedDate());
                }
            }
        }
    }
}   catch(
Exception e)

{
    System.err.println("ERROR : Error in Connecting to Remote Machine... Hence exitting...");       //
    e.printStackTrace();
    System.exit(2);
}

finally

{
    try {
        client.disconnect(true);
    } catch
            (Exception e) {
    }
}

Update

If "there is no active port for ftp , and my current finding is device is having only 4 active port i.e 80,443,3333,8192" seems file list send over HTTP and you can download it via HttpURLConnection and parse response. Something like this:

HttpURLConnection connection = null;
BufferedReader reader = null;

try {
    URL url = new URL("http://192.168.1.254");

    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.connect();

    int responseCode = connection.getResponseCode();

    InputStream stream = connection.getInputStream();

    reader = new BufferedReader(new InputStreamReader(stream));
    StringBuilder responseStringBuilder = new StringBuilder();

    String line = "";

    while ((line = reader.readLine()) != null) {
        responseStringBuilder .append(line);
        responseStringBuilder .append("\n");
    }

    // Parse responseStringBuilder.toString() (probably as HTML) here:
    ... 

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (connection != null) {
        connection.disconnect();
    }
    try {
        if (reader != null) {
            reader.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • i have tried (not programmatically but using filezilla )this earlier but found that there is no Active port for ftp , and my current finding is device is having only 4 active port i.e 80,443,3333,8192 – Shubham Jain Nov 27 '18 at 09:02
  • @ShubhamJain Please see update about `HttpURLConnection`. – Andrii Omelchenko Nov 27 '18 at 09:21
0

One way is to download html as a string from the server. Then use

 urlConnection = new URL("your_url").openConnection();
 reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

 while ((String line = reader.readLine()) != null) {
            stringBuilder.append(line).append("\n");
        }
 String your_data = Html.fromHtml(stringBuilder.toString());

This will contain the table in text format. You can process it to get the required data.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122