我在Android Studio中。
下面有两个异步任务。最上面一个是原始的,当我调用new ImageUploadTask()。execute();它按预期激发,我可以添加断点并逐步执行代码。我遇到了ftp代码问题,有人建议我从Apache切换到ftpj4。
我用Apache代码剪切了try catch块并添加到ftp4j代码中。现在我调用new ImageUploadTask()。execute();它永远不会进入异步任务。
我可以来回走动:添加Apache块并且它工作并且在ftp4j代码中它永远不会进入异步任务。如果我在ImageUploadTask()上设置一个断点.execute();然后按F8进入view.java而不是ImageUploadTask。
最底层是点击监听器,它永远不会改变。我在更改之间更改导入,同步gradle,清理和重建。
Apache代码
class ImageUploadTask extends AsyncTask <Void, Void, String>{
@Override
protected String doInBackground(Void... unused) {
try {
boolean status = false;
try {
FTPClient mFtpClient = new FTPClient();
String ip = "my domain dot com";
String userName = "ftp79815757-0";
String pass = "my password";
mFtpClient.connect(InetAddress.getByName(ip));
mFtpClient.login(userName, pass);
int reply = mFtpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
//one thread said this would do the trick
mFtpClient.enterLocalPassiveMode();
mFtpClient.enterRemotePassiveMode();
InputStream stream = new ByteArrayInputStream(imageData);
mFtpClient.changeWorkingDirectory("/images_in");
String currdir = mFtpClient.printWorkingDirectory();
if (!mFtpClient.storeFile("remoteName.jpg", stream)) {
Log.e("FTPUpload", String.valueOf(mFtpClient.getReplyCode()));
}
stream.close();
mFtpClient.disconnect();
}
else {
Log.e("FTPConnected", String.valueOf(reply));
}
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
} catch (Exception e) {
if (dialog.isShowing())
dialog.dismiss();
Toast.makeText(getApplicationContext(),
"Error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
return null;
}
}
ftp4j
class ImageUploadTask extends AsyncTask <Void, Void, String>{
@Override
protected String doInBackground(Void... unused) {
try {
FTPClient ftpClient = new FTPClient();
ftpClient.setType(FTPClient.TYPE_BINARY);
ftpClient.connect("my domain dot com");
ftpClient.login("ftp79815757-0", "my password");
InputStream stream = new ByteArrayInputStream(imageData);
ftpClient.upload("testsfile.jpg", stream,0,0,new MyFTPListener());
}
catch (Exception e) {
}
return null;
}
@Override
protected void onProgressUpdate(Void... unused) {
}
@Override
protected void onPostExecute(String sResponse) {
try {
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
点击监听器
upload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new ImageUploadTask().execute();
}
});