Download xml from FTP

2

I am trying with the following code to download several xml from an ftp. The first xml downloads it correctly but from the second it returns null . What would be the right way to do this?

private Vector<InputStream> descargarContenido()  {

    FTPClient mFTPClient = new FTPClient();
    Vector<InputStream> inputstreams = new Vector<InputStream>();
    try {
        mFTPClient.connect("ftp");

    mFTPClient.login("usuario","password");
    mFTPClient.enterLocalPassiveMode();
    mFTPClient.changeWorkingDirectory("/carpeta/xml");
    InputStream input1 = mFTPClient.retrieveFileStream("1.xml");
        inputstreams.add(input1);

    InputStream input2 = mFTPClient.retrieveFileStream("2.xml");
        inputstreams.add(input2);

    InputStream input3 = mFTPClient.retrieveFileStream("3.xml");
        inputstreams.add(input3);

    } catch (IOException e) {
        Toast.makeText(getApplicationContext(),"Error al descargar datos", Toast.LENGTH_LONG).show();

    }
    return inputstreams;
}

Edit:

I tried to use the code proposed by @Ajeno but I get an error reading the second xml, the first one loads the flow but the second one gets this error:

  

E / AndroidRuntime: FATAL EXCEPTION: AsyncTask # 2   java.lang.RuntimeException: An error occured while executing   doInBackground () at android.os.AsyncTask $ 3.done (AsyncTask.java:299) at   java.util.concurrent.FutureTask.finishCompletion (FutureTask.java:352)   at java.util.concurrent.FutureTask.setException (FutureTask.java:219)   at java.util.concurrent.FutureTask.run (FutureTask.java:239) at   android.os.AsyncTask $ SerialExecutor $ 1.run (AsyncTask.java:230) at   java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1080)   at   java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:573)   at java.lang.Thread.run (Thread.java:856) Caused by:   java.lang.NullPointerException at   org.apache.commons.io.IOUtils.copyLarge (IOUtils.java:2146) at   org.apache.commons.io.IOUtils.copy (IOUtils.java:2102) at   org.apache.commons.io.IOUtils.copyLarge (IOUtils.java:2123) at   org.apache.commons.io.IOUtils.copy (IOUtils.java:2078) at   MainActivity.downloadContent (MainActivity.java:544)   at   MainActivity.parsearXmlDeFTP (MainActivity.java:292)   at MainActivity.access $ 1000 (MainActivity.java:32)   at   MainActivity $ TaskDownloadXml.doInBackground (MainActivity.java:228)   at   MainActivity $ TaskDownloadXml.doInBackground (MainActivity.java:222)   at android.os.AsyncTask $ 2.call (AsyncTask.java:287) at   java.util.concurrent.FutureTask.run (FutureTask.java:234) at   android.os.AsyncTask $ SerialExecutor $ 1.run (AsyncTask.java:230) at   java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1080)   at   java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:573)   at java.lang.Thread.run (Thread.java:856) E / WindowManager: Activity   MainActivity has leaked window   com.android.internal.policy.impl.PhoneWindow $ DecorView {53459c08   V.E ..... R ...... D 0.0-1026,486} that was originally added here   android.view.WindowLeaked: Activity MainActivity   have leaked window   com.android.internal.policy.impl.PhoneWindow $ DecorView {53459c08   V.E ..... R ...... D 0.0-1026,486} that was originally added here at   android.view.ViewRootImpl. (ViewRootImpl.java:354) at   android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:216)   at android.view.WindowManagerImpl.addView (WindowManagerImpl.java:69)   at android.app.Dialog.show (Dialog.java:281) at   android.app.ProgressDialog.show (ProgressDialog.java:116) at   android.app.ProgressDialog.show (ProgressDialog.java:104) at   MainActivity.mostrarbarra (MainActivity.java:202)   at MainActivity.access $ 100 (MainActivity.java:32)   at MainActivity $ 6.onClick (MainActivity.java:174)   at android.view.View.performClick (View.java:4204) at   android.view.View $ PerformClick.run (View.java:17355) at   android.os.Handler.handleCallback (Handler.java:725) at   android.os.Handler.dispatchMessage (Handler.java:92) at   android.os.Looper.loop (Looper.java:137) at   android.app.ActivityThread.main (ActivityThread.java:5041) at   java.lang.reflect.Method.invokeNative (Native Method) at   java.lang.reflect.Method.invoke (Method.java:511) at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793)   at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:560) at   dalvik.system.NativeStart.main (Native Method) E / EGL_genymotion: egl:   current context mark for deletion

    
asked by F.Sola 19.07.2017 в 19:33
source

1 answer

1

The problem seems to be that you have the InputStream reference, in no time you close or give flush to the content. As its name says it is input flow, so the 3 files would travel on the same communication channel.

Try the following:

private List<byte[]> descargarContenido() {

    FTPClient mFTPClient = new FTPClient();
    List<byte[]> inputstreams = new ArrayList<byte[]>();
    try {
        mFTPClient.connect("ftp");

        mFTPClient.login("usuario","password");
        mFTPClient.enterLocalPassiveMode();
        mFTPClient.changeWorkingDirectory("/carpeta/xml");
        FTPFile[] filesArray = mFTPClient.listFiles();
        if (filesArray != null && filesArray.length > 0) {
            for (FTPFile file : filesArray) {
                if (!file.isFile()) {
                    continue;
                }
                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                mFTPClient.retrieveFile(file.getName(), baos);
                inputstreams.add(baos.toByteArray());
                baos.close();
            }
        }
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(),"Error al descargar datos", Toast.LENGTH_LONG).show();

    }
    return inputstreams;
}

I changed the Vector by List since that method is synchronized and brings with it a loss in performance and the java documentation recommends it.

link

The library is Apache you get from this place.

link

I relied on this link for multiple files.

    
answered by 19.07.2017 / 20:17
source