Code Monkey home page Code Monkey logo

webdavsyncerdemo's Introduction

WDSyncer

基于WebDav的安卓封装库

原博客:WebDav for Android

碎碎念

为啥要写这个库呢?

  • 尝试自己写一个库调用,学习一下这个流程,为以后做准备
  • WebDav同步过程很繁琐
  • 自己做的项目都会涉及到同步功能,不想重复写代码

更新日志

2023.06.09 0.0.6
修复依赖找不到的问题
2022.01.14 0.0.5
本版本没有新增内容
迁移库至JitPack
2021.02.01 0.0.4
新增支持自定义加密解密方式
2020.08.24 0.0.3
修复了查看路径文件服务器地址不生效的问题
调整了Demo中的部分逻辑
2020.08.22 0.0.2
修复上传文件失败的问题
新增clean,可以清除本地账户了

下次更新:增加大文件上传支持、上传进度回调

安全性

  • 对于账户和密码做了加密处理
  • 支持自定义加密方式

食用方法

在项目中引用即可 最新版本: Add it in your root build.gradle at the end of repositories:

allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

Step 2. Add the dependency

dependencies {
            implementation 'com.github.paul623:WebDavSyncerDemo:0.0.5'
    }

使用教程

Api接口

  /**
     * 上传文件
     * @param fileName 文件名 包含后缀名
     * @param fileLoc 文件目录 如:homeLoc/
     * @param listener 返回信息为 文件路径,上传成功
     * */
    public void uploadFile(String fileName, String fileLoc, File f, OnSyncResultListener listener);
    /**
     * 上传String类型数据
     * 你可以直接把文件格式设置为txt即可
     * @param fileName 文件名 包含后缀名
     * @param fileLoc 文件目录 如:homeLoc/
     * @param listener 返回信息为 文件路径,上传成功
     * */
    public void uploadString(String fileName, String fileLoc, String content, OnSyncResultListener listener);

   /**
    * 下载文件
    * @param listener 返回的是文件保存路径
    * 默认保存路径在:应用的私有路径下
    * */
    public void downloadFile(String fileName, String fileLoc, OnSyncResultListener listener);
    /**
     * 下载文件
     * @param listener 返回的是内容
     * */
    public void downloadString(String fileName, String fileLoc, OnSyncResultListener listener);

    /**
     * 列出所有文件信息
     * @param listFileListener 具体参看DavData
     * */
    public void listAllFile(String dir, OnListFileListener listFileListener);

    /**
     * 删除文件
     * @param fileDir 文件目录
     * */
    public void deleteFile(String fileDir, OnSyncResultListener listener);

使用示例

1.配置账户信息

使用SyncConfig来配置你的账户、密码以及服务器地址。

默认服务器地址为坚果云,即您可以不设置。

SyncConfig config=new SyncConfig(context);
config.setPassWord("你的密码");
config.setUserAccount("你的账户");

2.配置加密解密方式

💡0.0.4新增

如需要自己实现加密解密方式,请实现Encryption接口

public interface Encryption {
    //加密
    public String encode(String key);
    //解密
    public String decode(String password);
}

public class ExampleEncryption implements Encryption {
    @Override
    public String encode(String key) {
        return key+"&";
    }

    @Override
    public String decode(String password) {
        return password.split("&")[0];
    }
}

在配置的时候使用

 SyncConfig config=new SyncConfig(this,new DefaultEncryption());

同时,对应的SyncManager

 SyncManager syncManager=new SyncManager(this,new DefaultEncryption());

特别注意:SyncConfig传入Encryption后对应的SyncManager必须传入相同的Encryption,否则会抛出异常或者登录失败。

3.调用并实现回调

由于所有操作都必须在线程中执行,故你需要自行处理线程操作,这里以上传为例。

 SyncManager syncManager=new SyncManager(MainActivity.this);
        syncManager.uploadString("test.txt", "WDSyncer", "如你所见,WebDavSyncer已经配置成功!", new OnSyncResultListener() {
            @Override
            public void onSuccess(String result) {
                //成功
                Looper.prepare();
                Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
                Looper.loop();
            }

            @Override
            public void onError(String errorMsg) {
                //失败
                Looper.prepare();
                Toast.makeText(MainActivity.this,errorMsg,Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        });

或者你可以使用Handler来控制。

更多例子请查看项目代码。

文件上传下载我没有测试,如果有问题请在issue中提交告诉我

常见的出错类型

1.请求失败:出错了,javax.net.ssl.SSLHandshakeException: Chain validation failed

这个一般是手机的时间不对,导致握手失败。请检查模拟器或者手机的时间设置是否和当地地区时间相同

2.请求失败:出错了,com.thegrizzlylabs.sardineandroid.impl.SardineException: Error contacting https://dav.jianguoyun.com/dav/WDSyncer (401 )

这个是账户用户名或者密码不正确

3.Unable to start activity ComponentInfo{com.paul.webdavsyncerdemo/com.paul.webdavsyncerdemo.MainActivity}: java.lang.RuntimeException: 请配置Encryotion

这是由于你在SyncConfig中设置了Encryption而在SyncManager中没有设置

4.java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paul.webdavsyncerdemo/com.paul.webdavsyncerdemo.MainActivity}: java.lang.RuntimeException: Config中未配置Encryotion

这是由于你在SyncConfig没有配置Encryption而在SyncManager中设置了

兼容性

在安卓P及以上版本中,请在清单中配置:

 android:usesCleartextTraffic="true"

第三方依赖

本项目基于sardine

关于

@Paul623

Powered By 巴塞罗那的余晖

博客:https://www.cnblogs.com/robotpaul/

License

Copyright 2020 Paul623. https://github.com/paul623

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
 limitations under the License.

webdavsyncerdemo's People

Contributors

paul623 avatar left024 avatar

Stargazers

 avatar  avatar WYstudio avatar  avatar WALL.E avatar 徐金琦 avatar  avatar tangshg avatar Aylmer avatar Flandre Scarlet avatar  avatar  avatar  avatar Jack avatar ldlywt avatar  avatar  avatar  avatar  avatar W13KM avatar  avatar BGY avatar  avatar  avatar Janky avatar Alien avatar  avatar  avatar kobayashi avatar  avatar Yang_Jacker avatar  avatar  avatar  avatar KaffuChino avatar X avatar YanYu avatar shiguang1120 avatar 花有重开日 avatar  avatar yangxiaoge avatar  avatar  avatar  avatar 刘立正 avatar  avatar 面条君 avatar Meaty avatar  avatar

Watchers

 avatar  avatar

webdavsyncerdemo's Issues

Could not find sardine-android:0.7.

Caused by: org.gradle.internal.resolve.ModuleVersionNotFoundException: Could not find com.thegrizzlylabs.sardine-android:sardine-android:0.7.

列出文件失败

Java.io.IOExcetion not a valid dav respose

上传文件都没问题,列出文件就失败了

一点建议

无意间看到了文章然后看了下代码,顺便提点建议:

  1. 异步任务没必要用 Thread,太重了,可以考虑 Kotlin 协程
  2. 那个回调没必要搞 Looper 吧,新起的线程死循环了,Post 到主线程里就行了

提一个建议

new Thread 并发不好,建议改为线程池来处理,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.