gpt4 book ai didi

java - 代码可以在电脑上运行,但不能在手机上运行

转载 作者:行者123 更新时间:2023-12-02 07:33:42 25 4
gpt4 key购买 nike

我正在尝试使用 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

我已通过添加

授予应用程序使用互联网的权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

进入 list 文件。

为什么?

最佳答案

Caused by: android.os.NetworkOnMainThreadException 

SMB是网络访问。您正在 UI 线程上执行此操作。在 Android 3+ 中,您不能这样做。分离工作线程,或使用 AsyncTask

关于java - 代码可以在电脑上运行,但不能在手机上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12555298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com