【问题标题】:FTPClient cannot resolve host even with internet permission即使有 Internet 权限,FTPClient 也无法解析主机
【发布时间】:2019-05-02 10:43:08
【问题描述】:

我已将正确的权限添加到我的 AndroidManifest.XML 文件中,以使我能够从我的应用程序连接到互联网

<?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>

但是,当尝试像这样建立 FTP 连接时

FTPClient FtpClient = new FTPClient();

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

int reply = FtpClient.getReplyCode();

错误发生在FtpClient.connect(ftpServer + ftpFolder);

变量中的值格式为

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

我可以通过 PC 上的 Windows 文件资源管理器访问此文件夹,因此我知道它是有效路径,但在应用程序中进行测试时出现以下错误

无法解析主机“ftp://ftp.myServer.co.uk/mySite.co.uk/My 文件夹”:没有与主机名关联的地址

【问题讨论】:

    标签: java android ftp ftp-client


    【解决方案1】:

    FTPClient.connect 方法的参数(或者实际上是继承的SocketClient.connect)是hostname,而不是 URL。

    应该是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-02
      • 1970-01-01
      • 2017-11-19
      • 2021-06-15
      相关资源
      最近更新 更多