简体   繁体   English

代码在PC上工作,而不是在手机上

[英]Code working on PC, not on phone

I'm trying to fetch a list of files from a Windows share with jCIFS library. 我正在尝试使用jCIFS库从Windows共享中获取文件列表。 I have the following code from the examples from the jCIFS site. 我从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();
}

If I create a new project for PC and copy that into the main method, it will take a long time (around 12 seconds, why?) but it will return the file list without any errors. 如果我为PC创建一个新项目并将其复制到main方法中,则需要很长时间(大约12秒,为什么?)但它会返回文件列表而没有任何错误。 If I copy exactly the same code in my Android Activity, it will cause errors: 如果我在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

I've given the application permission to use internet by adding 我已经通过添加使用互联网的应用程序许可

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

into the manifest file. 进入清单文件。

Why? 为什么?

Caused by: android.os.NetworkOnMainThreadException 

SMB is network access. SMB是网络访问。 You're doing it on the UI thread. 你是在UI线程上做的。 In Android 3+, you can't. 在Android 3+中,你不能。 Spin off a worker thread, or use AsyncTask . 关闭工作线程,或使用AsyncTask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM