我用Jackrabbit lib实现webdav。
我使用以下代码使用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;
}
}
getContentType函数用于判断是文件夹还是文件。
我第一次运行ListWebDav方法是正常的。
但是如果第二次运行。
它会崩溃,并在行
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)
我该如何解决?
最佳答案
a)为此使用DAV:getcontenttype是不正确的;它是可选的(这可能在此处引起NullPointerException)。
b)您确实需要检查DAV:resourcetype属性。