Home

Awesome

简单易用的网络框架

集成步骤

allprojects {
    repositories {
        // JitPack 远程仓库:https://jitpack.io
        maven { url 'https://jitpack.io' }
    }
}
dependencyResolutionManagement {
    repositories {
        // JitPack 远程仓库:https://jitpack.io
        maven { url 'https://jitpack.io' }
    }
}
android {
    // 支持 JDK 1.8
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    // 网络请求框架:https://github.com/getActivity/EasyHttp
    implementation 'com.github.getActivity:EasyHttp:12.8'
    // OkHttp 框架:https://github.com/square/okhttp
    // noinspection GradleDependency
    implementation 'com.squareup.okhttp3:okhttp:3.12.13'
}

框架混淆规则

# OkHttp3 框架混淆规则
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
-dontwarn okio.**
# EasyHttp 框架混淆规则
-keep class com.hjq.http.** {*;}
# 必须要加上此规则,否则会导致泛型解析失败
-keep public class * implements com.hjq.http.listener.OnHttpListener {
    *;
}
# 必须要加上此规则,否则可能会导致 Bean 类的字段无法解析成后台返回的字段,xxx 请替换成对应包名
-keep class com.xxx.xxx.xxx.xxx.** {
    <fields>;
}

框架的具体用法请点击这里查看

不同网络请求框架之间的对比

功能或细节EasyHttpRetrofitOkGo
对应版本12.82.9.03.0.4
issues 数
aar 包大小95 KB123 KB131 KB
minSdk 要求API 14+API 21+API 14+
配置多域名
动态 Host
全局参数
日志打印
超时重试
配置 Http 缓存
下载文件校验
极速下载
下载断点续传
上传进度监听
Json 参数提交
Json 日志打印格式化
请求代码定位
延迟发起请求
分区存储适配
上传文件类型File / FileContentResolver <br> InputStream / RequestBodyRequestBodyFile
批量上传文件
请求生命周期自动管控需要封装需要封装
参数传值方式字段名 + 字段值参数名 + 参数值定义 Key + Value
框架灵活性
框架学习成本
API 记忆成本
接口维护成本
框架维护状态维护中维护中停止维护
public final class XxxApi implements IRequestApi {

    @Override
    public String getApi() {
        return "xxx/xxx";
    }

    private int xxx;

    public XxxApi setXxx(int xxx) {
        this.xxx = xxx;
        return this;
    }

    ......

    public final static class Bean {

        private int xyz;

        public int getXyz() {
            return xyz;
        }

        ......
    }
}

生命周期自动管控介绍

public final class HttpLifecycleManager implements LifecycleEventObserver {

    /**
     * 绑定组件的生命周期
     */
    public static void register(LifecycleOwner lifecycleOwner) {
        lifecycleOwner.getLifecycle().addObserver(new HttpLifecycleManager());
    }

    @Override
    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
        if (event != Lifecycle.Event.ON_DESTROY) {
            return;
        }

        // 移除监听
        source.getLifecycle().removeObserver(this);
        // 取消请求
        EasyHttp.cancelByTag(source);
    }
}

极速下载功能介绍

代码定位功能介绍

延迟发起请求功能介绍

EasyHttp.post(this)
        .api(new XxxApi())
        .delay(3000)
        .request(new HttpCallbackProxy<HttpData<XxxBean>>(this) {

            @Override
            public void onHttpSuccess(HttpData<XxxBean> result) {

            }
        });

作者的其他开源项目

微信公众号:Android轮子哥

Android 技术 Q 群:10047167

如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 :monkey_face:就太:thumbsup:了。您的支持将鼓励我继续创作:octocat:(点击查看捐赠列表

特别感谢

License

Copyright 2019 Huang JinQun

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.