我正在开发一个应用程序,一旦无线连接可用,该应用程序会将数据上传到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
答案 0 :(得分:0)
这可能是计时问题。在调试模式下,所有操作都会变慢,并且如果您有断点,则执行将在断点处停止。这可能会给其他线程一些时间来做其他事情,这隐藏了问题。从logcat看来,设备无法访问您的服务器。错误“ UnknownHostException”表示“ webhosting31.1blu.de”的DNS解析未返回IP地址。这意味着您没有连接到Internet,或者DNS配置已损坏。
如果您认为这是某种计时问题,则可以在尝试上传数据之前添加一些延迟,或者可以添加一些重试功能,该功能会稍等片刻,然后在上传失败时再次尝试。