javaandroidftpftp-client

FTPClient cannot resolve host even with internet permission


I've added the correct permission into my AndroidManifest.XML file to enable me connect to the internet from my app

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my_test_app">
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".LogOn"></activity>
    <activity android:name=".ftpDetails"/>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

But, when trying to establish an FTP connection like so

FTPClient FtpClient = new FTPClient();

FtpClient.connect(ftpServer + ftpFolder); // THIS IS WHERE THE ERROR OCCURS
FtpClient.login(ftpUser, ftpPass);

int reply = FtpClient.getReplyCode();

The error occurs on FtpClient.connect(ftpServer + ftpFolder);

The values in the variables are in the format of

ftpServer = ftp://ftp.myServer.co.uk/mySite.co.uk/
ftpFolder = My Folder (Test)

I can get to this folder through Windows File Explorer on my PC so I know it's a valid path, but when testing in the app I get the following error

Unable to resolve host "ftp://ftp.myServer.co.uk/mySite.co.uk/My Folder": No address associated with hostname


Solution

  • The argument of FTPClient.connect method (or actually the inherited SocketClient.connect is hostname, not URL.

    So it should be:

    ftpServer = "ftp.myServer.co.uk";