我正在开发一个应用程序,一旦无线连接可用,它就会将数据上传到 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$2.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$1.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$2.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$1.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