问题
I'm developing an application on Android that connects to a FTP server to upload and download files. To make the connection I'm using the apache commons-net library's FTPClient class based on this and I'm working on Eclipse.
But I get the following message on my Logcat:
07-04 21:11:44.196: D/USB Virtual(14708): Error: could not connect to host ftp://xxx.xxx
The following is my manifest permissions:
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
I'm really lost as to why I can't connect to my FTP server, in the console there're no errors showing. Am I missing something? I would thank any help.
I'm adding the code I'm using to connect to my FTP server:
public boolean ftpConnect(String host, String username, String password,
int port) {
try {
mFTPClient = new FTPClient();
//host is ftp://looner-project.zxq.net
mFTPClient.connect(host);
mFTPClient.connect(InetAddress.getByName(host));
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
boolean status = mFTPClient.login(username, password);
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
return status;
}
} catch (Exception e) {
Log.d(TAG, "Error: could not connect to host " + host);
}
return false;
}
回答1:
Things to check...
1. Check your internet connection.
2. Are you giving the Url to the ftp site correct ??
3. If its password protected, are you giving them correctly ??
4. Using command prompt check it. Give the below command to check it.
ftp www.your_ftp_site.com
after the above command you will be prompted for username and password.
I am also attaching my code which i used to upload audio file to the server to make it clear...
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import android.util.Log;
public class MyOwn {
public void goforIt(){
FTPClient con = null;
try
{
con = new FTPClient();
con.connect("www.mysamle.net"); // Its dummy Address
if (con.login("uju495", "Stevejobs!!"))
{
con.enterLocalPassiveMode(); // Very Important
con.setFileType(FTP.BINARY_FILE_TYPE); // Very Important
String data = "/sdcard/Vivekm4a.m4a";
FileInputStream in = new FileInputStream(new File(data));
boolean result = con.storeFile("/Ads/Vivekm4a.m4a", in);
in.close();
if (result) Log.v("upload result", "succeeded");
con.logout();
con.disconnect();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
来源:https://stackoverflow.com/questions/11337165/i-have-a-could-not-connect-to-host-logcat-message-while-trying-to-connect-to-f