Intent 被抛出,但动作只在 Debug 模式下完成

Intent is thrown, but action only done in Debug mode

我正在开发一个应用程序,一旦无线连接可用,它就会将数据上传到 FTP 服务器。 问题是,只有在调试模式下才会上传数据。

            if (sharedpreferences.getBoolean("autoupload",false)){
                // Do your work.
                String directory = context.getFilesDir().getAbsolutePath() + File.separator + "Messdaten";
                File f = new File(directory);
                File[] files = f.listFiles();
                if (files.length != 0 ){
                    for (int i = 0; i < files.length; i++) {
                        String filepath = files[i].toString();
                        FTPupload FTPu = new FTPupload(filepath);
                        FTPu.execute();
                        Toast.makeText(context, "BikeLog Daten werden hochgeladen", Toast.LENGTH_SHORT).show();
                    }
                } 
            }

显示了 Toast 消息,所以我确信其他所有内容也应该到达。但是当我通过设备资源管理器查看数据时,它仍然存在,尽管它应该在上传后被删除。

class FTPupload extends AsyncTask<Void, Void, String> {
    String path;
    ProgressDialog uploading;

    public FTPupload(String filepath) {
        path = filepath;
    }

    @Override
    protected String doInBackground(Void... params) {
        FTPSClient con = null;

        try {
            con = new FTPSClient();
            con.connect("XXXXXXXX");

            if (con.login("XXXXXXXXXX", "XXXXXXXXXXX")) {
                con.enterLocalPassiveMode(); // important!
                con.setFileType(FTP.BINARY_FILE_TYPE);
                FileInputStream in = new FileInputStream(new File(path));
                String filename = path.substring(path.lastIndexOf("/") + 1);
                boolean result = con.storeFile(filename, in);

                in.close();
                if (result) {
                    Log.v("upload result", "succeeded");
                    File file = new File(path);
                    boolean deleted = file.delete();
                }
                con.logout();
                con.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }
}

疯狂的是,一旦我将它插入我的计算机并在 FTP 连接中放置一个断点,一切都按计划进行,数据被发送到服务器。

你能帮我找出问题所在吗?任何提示都很棒!

编辑: Logcat 是这么说的:

2019-02-13 15:43:01.347 1537-1906/? D/ConnectivityService: Switching to new default network: NetworkAgentInfo{ ni{[type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "ReineLernWG", failover: false, available: true, roaming: false, metered: false]}  network{123}  nethandle{528297413342}  lp{{InterfaceName: wlan0 LinkAddresses: [fe80::f2d7:aaff:fe3c:6a11/64,192.168.178.57/24,]  Routes: [fe80::/64 -> :: wlan0,192.168.178.0/24 -> 0.0.0.0 wlan0,0.0.0.0/0 -> 192.168.178.1 wlan0,] DnsAddresses: [192.168.178.1,] Domains: fritz.box MTU: 0 TcpBufferSizes: 131072,262144,3145728,4096,221184,3145728}}  nc{[ Transports: WIFI Capabilities: NOT_METERED&INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN LinkUpBandwidth>=1048576Kbps LinkDnBandwidth>=1048576Kbps SignalStrength: -56]}  Score{20}  everValidated{false}  lastValidated{false}  created{true} lingering{false} explicitlySelected{false} acceptUnvalidated{false} everCaptivePortalDetected{false} lastCaptivePortalDetected{false} }
2019-02-13 15:43:01.355 18331-18450/online.simpledesign.bikelog W/System.err: java.net.UnknownHostException: webhosting31.1blu.de
2019-02-13 15:43:01.355 18331-18450/online.simpledesign.bikelog W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:183)
2019-02-13 15:43:01.355 18331-18450/online.simpledesign.bikelog W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
2019-02-13 15:43:01.355 18331-18450/online.simpledesign.bikelog W/System.err:     at java.net.Socket.connect(Socket.java:586)
2019-02-13 15:43:01.355 18331-18450/online.simpledesign.bikelog W/System.err:     at org.apache.commons.net.SocketClient.connect(SocketClient.java:176)
2019-02-13 15:43:01.356 18331-18450/online.simpledesign.bikelog W/System.err:     at org.apache.commons.net.SocketClient.connect(SocketClient.java:268)
2019-02-13 15:43:01.356 18331-18450/online.simpledesign.bikelog W/System.err:     at online.simpledesign.bikelog.FTPupload.doInBackground(WifiUploader.java:67)
2019-02-13 15:43:01.356 18331-18450/online.simpledesign.bikelog W/System.err:     at online.simpledesign.bikelog.FTPupload.doInBackground(WifiUploader.java:53)
2019-02-13 15:43:01.356 18331-18450/online.simpledesign.bikelog W/System.err:     at android.os.AsyncTask.call(AsyncTask.java:304)
2019-02-13 15:43:01.356 18331-18450/online.simpledesign.bikelog W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
2019-02-13 15:43:01.356 18331-18450/online.simpledesign.bikelog W/System.err:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:243)
2019-02-13 15:43:01.356 18331-18450/online.simpledesign.bikelog W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
2019-02-13 15:43:01.357 18331-18450/online.simpledesign.bikelog W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
2019-02-13 15:43:01.357 18331-18450/online.simpledesign.bikelog W/System.err:     at java.lang.Thread.run(Thread.java:761)
2019-02-13 15:43:01.357 18331-18450/online.simpledesign.bikelog D/exception: java.net.UnknownHostException: webhosting31.1blu.de
2019-02-13 15:43:01.359 18331-18454/online.simpledesign.bikelog W/System.err: java.net.UnknownHostException: webhosting31.1blu.de
2019-02-13 15:43:01.359 18331-18454/online.simpledesign.bikelog W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:183)
2019-02-13 15:43:01.359 18331-18454/online.simpledesign.bikelog W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
2019-02-13 15:43:01.360 18331-18454/online.simpledesign.bikelog W/System.err:     at java.net.Socket.connect(Socket.java:586)
2019-02-13 15:43:01.360 18331-18454/online.simpledesign.bikelog W/System.err:     at org.apache.commons.net.SocketClient.connect(SocketClient.java:176)
2019-02-13 15:43:01.360 18331-18454/online.simpledesign.bikelog W/System.err:     at org.apache.commons.net.SocketClient.connect(SocketClient.java:268)
2019-02-13 15:43:01.360 18331-18454/online.simpledesign.bikelog W/System.err:     at online.simpledesign.bikelog.FTPupload.doInBackground(WifiUploader.java:67)
2019-02-13 15:43:01.360 18331-18454/online.simpledesign.bikelog W/System.err:     at online.simpledesign.bikelog.FTPupload.doInBackground(WifiUploader.java:53)
2019-02-13 15:43:01.360 18331-18454/online.simpledesign.bikelog W/System.err:     at android.os.AsyncTask.call(AsyncTask.java:304)
2019-02-13 15:43:01.360 18331-18454/online.simpledesign.bikelog W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
2019-02-13 15:43:01.360 18331-18454/online.simpledesign.bikelog W/System.err:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:243)
2019-02-13 15:43:01.361 18331-18454/online.simpledesign.bikelog W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
2019-02-13 15:43:01.361 18331-18454/online.simpledesign.bikelog W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
2019-02-13 15:43:01.361 18331-18454/online.simpledesign.bikelog W/System.err:     at java.lang.Thread.run(Thread.java:761)
2019-02-13 15:43:01.361 18331-18454/online.simpledesign.bikelog D/exception: java.net.UnknownHostException: webhosting31.1blu.de

这可能是时间问题。在调试模式下,一切都运行得更慢,如果你有一个断点,执行会在断点处停止。这可能会让其他线程有时间做其他事情,这会隐藏问题。从 logcat 可以清楚地看出设备无法访问您的服务器。错误 "UnknownHostException" 表示 "webhosting31.1blu.de" 的 DNS 解析未返回 IP 地址。这意味着您没有连接到 Internet,或者 DNS 配置已损坏。

如果您认为这是某种计时问题,您可以在尝试上传数据之前添加一些延迟,或者您可以添加一些重试功能,等待一段时间并在上传失败时重试。