0

我正在为 Android 开发一个 samba 客户端。给定一个 IP 地址,它应该连接到它并浏览共享文件夹。

为此,我使用JCIFS。我将 jar 放到我的 Android 项目中,并添加了以下代码以连接到 PC 并获取文件列表:

private void connectToPC() throws IOException {
    String ip = "x.x.x.x";
    String user = Constants.username + ":" + Constants.password;
    String url = "smb://" + ip;

    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
    SmbFile root= new SmbFile(url, auth);

    String[] files = root.list();
    for (String fileName : files) {
        Log.d("GREC", "File: " + fileName);
    }
}

我得到回报:jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password。

但是凭据是正确的。我还尝试使用来自 android 市场的另一个使用 JCIFS 的 samba 客户端,它成功连接到该 IP,所以显然我在这里做错了,但不知道特别是什么。

非常感谢任何帮助。

4

3 回答 3

1

最后我成功连接到PC。问题出在NtlmPasswordAuthentication();构造函数中。

所以,而不是这个:

String user = Constants.username + ":" + Constants.password;
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);

我改成这样:

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",
                    Constants.username, Constants.password);

不知道为什么,可能是“:”特殊字符的原因,可能是Android的原因,但是将一个空的域名、用户名和密码分别传递给构造函数,解决了这个问题。

于 2013-03-20T12:33:15.257 回答
1

由于有些人在遇到 android 和 JCIFS 的类似问题时会谈到这个话题,所以这些是尝试使其工作时的其他常见问题:

*将 .jar 专门放在你的 android 项目的 /libs 文件夹中(不仅仅是通过“构建路径”)

*确保您的项目有互联网权限我需要什么权限才能从 android 应用程序访问互联网?

*还要确保您的 JCIFS 代码在与 UI 不同的线程中运行(换句话说,使用 AsyncTask 类)如何在 android 中使用 AsyncTask 中的方法?

*代码:

 protected String doInBackground(String... params) {

          SmbFile[] domains;
           String username = USERNAME;
           String password = PASSWORD;
           NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",
                username, password);

            try {
                SmbFile sm = new SmbFile(SMB_URL, auth);
                domains = sm.listFiles();
                for (int i = 0; i < domains.length; i++) {

                    SmbFile[] servers = domains[i].listFiles();
                    for (int j = 0; j < servers.length; j++) {
                       Log.w(" Files ", "\t"+servers[j]);
                    }
                }
            } catch (SmbException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            return "";
    }

这些是我在尝试在 android 上运行 JCIFS 时遇到的问题,希望对任何人有所帮助,问候。

于 2013-05-22T16:50:42.057 回答
1

也许我也可以帮助其他人。

我遇到的问题是我使用 thread.run() 而不是 thread.start() 在 Runnable 中执行 Smb-Code。我花了很多时间寻找答案,但没有解决我的问题。

但后来一位朋友向我解释了 thread.run() 和 thread.start() 之间的区别:

run():像普通方法一样执行 Methode(例如 Runnable 的 run() Methode)(同步)

start():在自己的任务中使用 Runnable 启动线程(异步)

对于 Smb,您需要一个异步线程。因此,您需要调用 thread.start()!

也许有人犯了和我一样的错误。

于 2014-01-29T08:58:47.673 回答