I have a question related to Http. In android how to list the files of a folder which is in Http server.
I am able to Download file from server. But I want to check fil
You will need a server side script to list the files such as this one in PHP
Place the file in the directory with all the files you want to see, called listfiles.php
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
//$thelist = $thelist.$file.'<br>';
$thelist = $thelist.$file."\r\n";
}
}
closedir($handle);
}
echo $thelist;
?>
Then in your android java app, you will download the file, and then build an array of the filenames for your processing.
String serverFile = "path on your server/listfiles.php";
String listOfFiles = DownloadText(serverFile);
String[] linesOfFiles = listOfFiles.split("<br>");
Then you have a string array of all of the files on the server. I have not included the code for the DownloadText function, but that is very standard and you can find it easily. It will download any text file over HTTP.
HTH
Server script to get a list of files as a string separated by "-" :
<?php
$path = realpath('your path\\your folder');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
if (strpos($filename,'.mp3') !== false)
$filenm=$filenm ."-". basename($filename);
}
echo $filenm
?>
Android code to get string from server and separate the filenames:
try {
int timeout= 7000;
HttpPost httppost= null;
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, timeout);
httppost = new HttpPost("http://yourserver/getlist.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e) {
e.printStackTrace();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e){
e.printStackTrace();
}
for (String getFile: result.split("-")){
Log.d("abc","String is:" + getFile);
}
Create a web service method on your target server that (in Java) calls something like
File myDir = new File(request.getRealPath("pathToMyDir"));
ArrayList fileArray = new ArrayList(Arrays.asList(myDir.list()));
The 'getRealPath' method is deprecated, but works for me ;-) My files are in a folder within my web application. Your method will need to package up the contents of the ArrayList in a JSON string (as the return).
Then, create a JSON call to this method within Android, and parse the results in the normal way. Plenty of SO tickets describing that mechanism. HTH.