@Koleostr

Как правильно выводить названия файлов с сервера в listview?

Всем доброго времени суток. В приложении нужно подключиться к FTP серверу, после чего в listview должны выводиться файлы, лежащие на сервере, к которому я подключаюсь. Проблема в том, что файлы с сервера не выводятся в listview, если вообще происходит подключение к серверу. В логах никаких ошибок нет. В чем может быть проблема?

Вообще, у меня есть подозрение, что AsynkTask вообще не запускается.

Код java класса:
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;

public class ftpClient extends Fragment {

    FTPClient ftpClient;

    Button button;
    EditText textIp,textPort,textPassword,textLogin;
    TextView printDir;
    ListView listFolder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_ftpclient, container, false);

        textIp =(EditText)rootView.findViewById(R.id.editText5);
        textPort =(EditText)rootView.findViewById(R.id.editText6);
        textPassword =(EditText)rootView.findViewById(R.id.editText7);
        textLogin =(EditText)rootView.findViewById(R.id.editText8);
        listFolder = (ListView) rootView.findViewById(R.id.listView);
        printDir = (TextView) rootView.findViewById(R.id.textView2);


        button=(Button) rootView.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try{
                    programTask  prTask = new programTask(textIp.getText().toString(), Integer.parseInt(textPort.getText().toString()), textLogin.getText().toString(), textPassword.getText().toString());
                    prTask.execute();

                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });

        return rootView;
    }

    private class programTask extends AsyncTask<String, ArrayList<String>, Void> {

        ArrayAdapter<String> adapter;

        private String ip,login,pass;
        private int port;

        programTask(String ip, int port, String login,String pass){
            this.ip=ip;
            this.port=port;
            this.login=login;
            this.pass=pass;
        }

        @Override
        protected Void doInBackground(String... urls) {
            try{

                ftpClient = new FTPClient();
                ftpClient.connect(InetAddress.getByName(ip),port);
                ftpClient.login(login, pass);
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();

                ArrayList<String> listItems = new ArrayList<String>();
                FTPFile[] ftpFiles = ftpClient.listFiles();
                for (FTPFile file : ftpFiles) {
                    listItems.add(file.getName());
                }

                publishProgress(listItems);
                ftpClient.logout();
                ftpClient.disconnect();
            }catch (IOException e){
                e.printStackTrace();

            }
            return null;
        }

        @SafeVarargs
        @Override
        protected final void onProgressUpdate(ArrayList<String>... listitem) {
            super.onProgressUpdate(listitem);

            adapter=new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, listitem[0]);
            listFolder.setAdapter(adapter);

        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

        }
    }
}


Код xml файла:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Введите идентификаторы сервера"
        android:textSize="18sp"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <EditText
        android:id="@+id/editText5"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Адрес сервера"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <EditText
        android:id="@+id/editText6"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="6dp"
        android:ems="10"
        android:hint="Порт"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText5"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <EditText
        android:id="@+id/editText7"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Логин"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText6"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <EditText
        android:id="@+id/editText8"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Пароль"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText7"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Подключиться к серверу"
        android:textSize="18sp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText8"
        android:background="#b5b6b8"
        android:padding="5dp"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text=""
        android:textColor="#000000"
        android:textSize="18sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <ListView
        android:id="@+id/listView"
        style="@style/Widget.AppCompat.ListView"
        android:layout_width="350dp"
        android:layout_height="180dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="6dp"
        android:background="#b5b6b8"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

</android.support.constraint.ConstraintLayout>
  • Вопрос задан
  • 221 просмотр
Пригласить эксперта
Ответы на вопрос 1
thelongrunsmoke
@thelongrunsmoke
Программист
1) Начинай с проверки работы InetAddress. Скорее всего адрес сервера не ресолвится, по той или иной причине.
2) listFiles() возвращает содержимое только текущей рабочей директории.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы