Listing files of a folder present in the http server

后端 未结 3 933
清酒与你
清酒与你 2021-01-22 19:11

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

3条回答
  •  失恋的感觉
    2021-01-22 19:52

    Server script to get a list of files as a string separated by "-" :

    
    

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

提交回复
热议问题