我正在开发一个应用程序,一旦无线连接可用,就会将数据上传到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消息,所以我确信,其他所有内容都应该被访问。但是当我通过Device explorer检查数据时,它仍然存在,尽管它应该在上传后删除。
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
这可能是一个时间问题。在调试模式下,一切都运行得很慢,如果你有一个断点,执行会在断点处停止。这可能会让其他线程有时间做其他事情,这就隐藏了这个问题。从logcat可以清楚地看出,设备无法访问您的服务器。错误“UnknownHostException”表示“webhosting31.1blu.de”的DNS解析未返回IP地址。这意味着您未连接到Internet,或者DNS配置已损坏。
如果您认为这是某种计时问题,您可以在尝试上载数据之前添加一些延迟,或者您可以添加一些等待一段时间的重试功能,如果上载失败则再次尝试。