OkHttpFinal

Introduction: 一个对 OkHttp 封装的简单易用型 HTTP 请求和文件下载管理框架
More: Author   ReportBugs   DemoAPK   
Tags:

一个对 OkHttp 封装的简单易用型 HTTP 请求和文件下载管理框架。更多详情请查看WIKI

  • 简化OkHttp使用
  • 支持 GET,POST,PUT,DELETE,HEAD,PATCH 谓词
  • 支持 Activity 和 Fragment 生命周期结束后终止请求
  • 支持 Download Manager 功能
  • 支持文件下载多事件回调
  • 支持返回 bean 对象
  • 支持返回 json String 数据
  • 支持返回 JsonObject 对象
  • 支持 https 请求
  • 支持 https 证书访问
  • 支持文件上传
  • 支持全局 params
  • 支持全局 header
  • 支持 http cancel
  • ……

下载 OkHttpFinal

下载这个JAR 或者通过 Gradle 抓取:

compile 'cn.finalteam:okhttpfinal:2.0.7'

eclipse 使用

下载 OkHttpFinal 对应的Jar 和下载依赖Extra Jar


Demo apk:DEMO APK

如何使用

1、在你 App Application 中初始化 OkHttpFinal(此初始化只是简单赋值不会阻塞线程)

OkHttpFinalConfiguration.Builder builder = new OkHttpFinalConfiguration.Builder();
OkHttpFinal.getInstance().init(builder.build());

2、请求接口

List<File> files = new ArrayList<>();
File file = new File("...");
RequestParams params = new RequestParams(this);//请求参数
params.addFormDataPart("username", mUserName);//表单数据
params.addFormDataPart("password", mPassword);//表单数据
params.addFormDataPart("file", file);//上传单个文件
params.addFormDataPart("files", files);//上传多个文件
params.addHeader("token", token);//添加 header 信息
HttpRequest.post(Api.LOGIN, params, new BaseHttpRequestCallback<LoginResponse>() {

//请求网络前
@Override
public void onStart() {
    buildProgressDialog().show();
}

//这里只是网络请求成功了(也就是服务器返回 JSON 合法)没有没有分装具体的业务成功与失败,大家可以参考 demo 去分装自己公司业务请求成功与失败
@Override
protected void onSuccess(LoginResponse loginResponse) {
    toast(loginResponse.getMsg());
}

//请求失败(服务返回非法 JSON、服务器异常、网络异常)
@Override
public void onFailure(int errorCode, String msg) {
    toast("网络异常~,请检查你的网络是否连接后再试");
}

//请求网络结束   
@Override
public void onFinish() {
    dismissProgressDialog();
}

下载文件

String url = "http://219.128.78.33/apk.r1.market.hiapk.com/data/upload/2015/05_20/14/com.speedsoftware.rootexplorer_140220.apk";
File saveFile = new File("/sdcard/rootexplorer_140220.apk");
HttpRequest.download(url, saveFile, new FileDownloadCallback() {
    //开始下载
    @Override 
    public void onStart() {
        super.onStart();
    }

    //下载进度
    @Override
    public void onProgress(int progress, long networkSpeed) {
        super.onProgress(progress, networkSpeed);
        mPbDownload.setProgress(progress);
        //String speed = FileUtils.generateFileSize(networkSpeed);
    }

    //下载失败
    @Override 
    public void onFailure() {
        super.onFailure();
        Toast.makeText(getBaseContext(), "下载失败", Toast.LENGTH_SHORT).show();
    }

    //下载完成(下载成功)
    @Override 
    public void onDone() {
        super.onDone();
        Toast.makeText(getBaseContext(), "下载成功", Toast.LENGTH_SHORT).show();
    }
});

更多功能请查看WIKI

代码混淆

#--------------- BEGIN: okhttp ----------
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**
#--------------- END: okhttp ----------
#--------------- BEGIN: okio ----------
-keep class sun.misc.Unsafe { *; }
-dontwarn java.nio.file.*
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-dontwarn okio.**
#--------------- END: okio ----------

更新日志

关于作者

License

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.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools