brian
brian

Reputation: 6912

Webdav with jackrabbit lib on android

I implement webdav with Jackrabbit lib.
And I use below code to scan all files on folder with webdav:

public void ListWebDav() {
    String FullPath = Webdav Path;
    try {
        DavMethod davMethod = new PropFindMethod(FullPath, DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1);
        WebDavClient.executeMethod(davMethod);

        MultiStatus multiStatus = davMethod.getResponseBodyAsMultiStatus();
        MultiStatusResponse[] responses = multiStatus.getResponses();
        MultiStatusResponse currResponse;

        for(int i = 0; i < responses.length; i++) {
            currResponse = responses[i];
            if(getContentType(currResponse) != null) {
                if(((String)getContentType(currResponse)).contains("directory")) {  //Folder
                    System.out.println("Folder " + URLDecoder.decode(currResponse.getHref()));
                }
                else {  //File
                    System.out.println("File " + URLDecoder.decode(currResponse.getHref()));
                }
            }
            else {
                System.out.println(URLDecoder.decode(currResponse.getHref()));
            }
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

public Object getContentType(MultiStatusResponse msr) {
    try {
        Status[] statusa = msr.getStatus();
        Status status = statusa[0];

        int n = status.getStatusCode();
        DavPropertySet propSet = msr.getProperties(n);
        DavProperty davProp = propSet.get(DavPropertyName.PROPERTY_GETCONTENTTYPE);
        return davProp.getValue();
    }
    catch(Exception e) {
        return null;
    }
}

The getContentType function is to judge is folder or file.
I run the ListWebDav method first time is works normal.
But if run the second times.
It will crash with below error code on line Status[] statusa = msr.getStatus();:

11-27 10:08:29.331: E/StrictMode(26469): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
11-27 10:08:29.331: E/StrictMode(26469): java.lang.Throwable: Explicit termination method 'end' not called
11-27 10:08:29.331: E/StrictMode(26469):    at dalvik.system.CloseGuard.open(CloseGuard.java:184)
11-27 10:08:29.331: E/StrictMode(26469):    at java.util.zip.Inflater.<init>(Inflater.java:82)
11-27 10:08:29.331: E/StrictMode(26469):    at java.util.zip.ZipFile.getInputStream(ZipFile.java:310)
11-27 10:08:29.331: E/StrictMode(26469):    at java.util.jar.JarFile.getInputStream(JarFile.java:389)
11-27 10:08:29.331: E/StrictMode(26469):    at libcore.net.url.JarURLConnectionImpl.getInputStream(JarURLConnectionImpl.java:222)
11-27 10:08:29.331: E/StrictMode(26469):    at java.net.URL.openStream(URL.java:470)
11-27 10:08:29.331: E/StrictMode(26469):    at java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:432)
11-27 10:08:29.331: E/StrictMode(26469):    at java.lang.Class.getResourceAsStream(Class.java:1037)
11-27 10:08:29.331: E/StrictMode(26469):    at org.apache.jackrabbit.webdav.DavException.<clinit>(DavException.java:41)
11-27 10:08:29.331: E/StrictMode(26469):    at org.apache.jackrabbit.webdav.Status.<init>(Status.java:42)
11-27 10:08:29.331: E/StrictMode(26469):    at org.apache.jackrabbit.webdav.MultiStatusResponse.getStatus(MultiStatusResponse.java:253)
11-27 10:08:29.331: E/StrictMode(26469):    at com.wd.wifi.cable.GlobalData.getContentType(GlobalData.java:313)
11-27 10:08:29.331: E/StrictMode(26469):    at com.wd.wifi.cable.GlobalData.ListWebDav(GlobalData.java:277)
11-27 10:08:29.331: E/StrictMode(26469):    at com.wd.wifi.cable.GlobalData.ConnectWebDav(GlobalData.java:258)
11-27 10:08:29.331: E/StrictMode(26469):    at com.wd.wifi.cable.HDDDevicesListViewFragment$3.onItemClick(HDDDevicesListViewFragment.java:81)
11-27 10:08:29.331: E/StrictMode(26469):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
11-27 10:08:29.331: E/StrictMode(26469):    at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
11-27 10:08:29.331: E/StrictMode(26469):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
11-27 10:08:29.331: E/StrictMode(26469):    at android.widget.AbsListView$3.run(AbsListView.java:3638)
11-27 10:08:29.331: E/StrictMode(26469):    at android.os.Handler.handleCallback(Handler.java:733)
11-27 10:08:29.331: E/StrictMode(26469):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-27 10:08:29.331: E/StrictMode(26469):    at android.os.Looper.loop(Looper.java:137)
11-27 10:08:29.331: E/StrictMode(26469):    at android.app.ActivityThread.main(ActivityThread.java:4998)
11-27 10:08:29.331: E/StrictMode(26469):    at java.lang.reflect.Method.invokeNative(Native Method)
11-27 10:08:29.331: E/StrictMode(26469):    at java.lang.reflect.Method.invoke(Method.java:515)
11-27 10:08:29.331: E/StrictMode(26469):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-27 10:08:29.331: E/StrictMode(26469):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-27 10:08:29.331: E/StrictMode(26469):    at dalvik.system.NativeStart.main(Native Method)

How can I fix it?

Upvotes: 1

Views: 792

Answers (1)

Julian Reschke
Julian Reschke

Reputation: 42025

a) It's incorrect to use DAV:getcontenttype for this; it's optional (and that's probably causing a NullPointerException here). b) You really need to check the DAV:resourcetype property.

Upvotes: 1

Related Questions