我正在尝试使用 jCIFS 库从 Windows 共享中获取文件列表。我从 jCIFS 站点的示例中获得了以下代码。
try {
String p = "smb://domain.tld/path/to/the/folder/";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "username", "password");
SmbFile file;
SmbFile[] files = new SmbFile[0];
file = new SmbFile(p, auth);
long t1 = System.currentTimeMillis();
try {
files = file.listFiles();
} catch (Exception e) {
e.printStackTrace();
}
long t2 = System.currentTimeMillis() - t1;
for( int i = 0; i < files.length; i++ ) {
System.out.println(files[i].getName() );
}
System.out.println();
System.out.println( files.length + " files in " + t2 + "ms" );
} catch(Exception e) {
e.printStackTrace();
}
如果我为 PC 创建一个新项目并将其复制到 main 方法中,这将需要很长时间(大约 12 秒,为什么?)但它会返回文件列表而没有任何错误。如果我在我的 Android Activity 中复制完全相同的代码,则会导致错误:
09-23 21:49:08.605: E/AndroidRuntime(32659): FATAL EXCEPTION: main
09-23 21:49:08.605: E/AndroidRuntime(32659): java.lang.ExceptionInInitializerError
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:864)
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.smb.SmbFile.connect(SmbFile.java:951)
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.smb.SmbFile.resolveDfs(SmbFile.java:669)
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.smb.SmbFile.send(SmbFile.java:773)
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.smb.SmbFile.doFindFirstNext(SmbFile.java:1986)
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.smb.SmbFile.doEnum(SmbFile.java:1738)
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.smb.SmbFile.listFiles(SmbFile.java:1715)
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.smb.SmbFile.listFiles(SmbFile.java:1648)
09-23 21:49:08.605: E/AndroidRuntime(32659): at fi.peltoset.mikko.mpsharepoint.SharedFiles.onCreate(SharedFiles.java:46)
09-23 21:49:08.605: E/AndroidRuntime(32659): at android.app.Activity.performCreate(Activity.java:4543)
09-23 21:49:08.605: E/AndroidRuntime(32659): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
09-23 21:49:08.605: E/AndroidRuntime(32659): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
09-23 21:49:08.605: E/AndroidRuntime(32659): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
09-23 21:49:08.605: E/AndroidRuntime(32659): at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-23 21:49:08.605: E/AndroidRuntime(32659): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
09-23 21:49:08.605: E/AndroidRuntime(32659): at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 21:49:08.605: E/AndroidRuntime(32659): at android.os.Looper.loop(Looper.java:156)
09-23 21:49:08.605: E/AndroidRuntime(32659): at android.app.ActivityThread.main(ActivityThread.java:5045)
09-23 21:49:08.605: E/AndroidRuntime(32659): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 21:49:08.605: E/AndroidRuntime(32659): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 21:49:08.605: E/AndroidRuntime(32659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-23 21:49:08.605: E/AndroidRuntime(32659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-23 21:49:08.605: E/AndroidRuntime(32659): at dalvik.system.NativeStart.main(Native Method)
09-23 21:49:08.605: E/AndroidRuntime(32659): Caused by: java.lang.ExceptionInInitializerError
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.UniAddress.<clinit>(UniAddress.java:62)
09-23 21:49:08.605: E/AndroidRuntime(32659): ... 24 more
09-23 21:49:08.605: E/AndroidRuntime(32659): Caused by: android.os.NetworkOnMainThreadException
09-23 21:49:08.605: E/AndroidRuntime(32659): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1190)
09-23 21:49:08.605: E/AndroidRuntime(32659): at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
09-23 21:49:08.605: E/AndroidRuntime(32659): at java.net.InetAddress.getLocalHost(InetAddress.java:374)
09-23 21:49:08.605: E/AndroidRuntime(32659): at jcifs.netbios.NbtAddress.<clinit>(NbtAddress.java:187)
09-23 21:49:08.605: E/AndroidRuntime(32659): ... 25 more
我已通过添加授予应用程序使用 Internet 的权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
进入清单文件。
为什么?