RetrofitClient

项目地址:Jadyli/RetrofitClient
简介:基于 OKHttp 和 Retrofit 封装的网络库
更多:作者   提 Bug   
标签:
retrofit-okhttp-

RetrofitClient 基于 OKHttp 和 Retrofit 封装了基本的 GET、POST、PUT、DELET 请求和文件上传下载方法,只需三步,快速集成。

1 在 app 或 lib 级别的 build.gradle 文件中添加依赖

compile 'com.jady:retrofitclient:0.2.6'

2 在 MainApplication 中初始化

HttpManager.init(this, UrlConfig.BASE_URL);
HttpManager.getInstance().setOnGetHeadersListener(new HttpManager.OnGetHeadersListener() {
      @Override
      public Map<String, String> getHeaders() {
          Map<String, String> headers = new HashMap<>();
          headers.put("access_token", "1234");
          return headers;
      }
});

init(Context context, String baseUrl)方法用来传项目的基础 url,当然具体的请求也可以动态修改。 另一个是所有请求都需要添加的的请求头,比如accessTokendeviceKey这种。

3 自动义请求回调

参考: 1.ServerCallback 2.CommonCallback

具体封装过程参考我的另一篇文章android 使用 gson 和泛型解析服务器回调的封装

使用

参考: 1.实例中的 API 类 2.实例中的 BaseRequestFragment 类

GET 请求

API.testGet(new CommonCallback<Feed>() {

     @Override
     public void onSuccess(Feed feed) {
           tvBaseRequestGet.setText(feed.getContent);
     }

     @Override
     public void onFailure(String error_code, String error_message) {

     }
});

不使用baseUrl:

HttpManager.getFullPath(String fullUrl, Map<String, Object> parameters, HttpCallback callback)

POST 请求

API.testPost(name, password, new ServerCallback<ServerCallbackModel<String>, String>() {

    @Override
    public void onSuccess(String accessToken) {
        showToast("accessToken:" + accessToken);
    }

    @Override
    public void onFailure(String error_code, String error_message) {
    }

    /**
    * 对单个请求允许 Toast 显示错误信息
    */
    @Override
    public boolean enableShowToast() {
        return true;
    }
});

不使用baseUrl:

HttpManager.postFullPath(String fullUrl, Map<String, Object> parameters, HttpCallback callback)

post 对象,以 json 传输

HttpManager.postByBody(String url, T body, HttpCallback callback)

PUT 请求

API.testPut(putContent, new ServerCallback<ServerCallbackModel<Object>, Object>() {

    @Override
    public void onSuccess(Object o) {
        showToast("更新成功");
    }

    @Override
    public void onFailure(String error_code, String error_message) {
        showToast("更新失败,请检查 token");
    }
});

DELETE 请求

API.testDelete(1,new ServerCallback<ServerCallbackModel<Object>, Object>() {
    @Override
    public void onSuccess(Object o) {
        showToast("删除成功");
    }

    @Override
    public void onFailure(String error_code, String error_message) {
        showToast("删除失败,请检查 token");
    }
});

文件上传 单文件上传:

HttpManager.uploadFile(String url, String filePath, String fileDes, TransformProgressListener iProgress)

多文件上传:

HttpManager.uploadFiles(String url, List<String> filePathList, TransformProgressListener iProgress)

如果需要临时改变baseUrl,只需要在发送请求之前调用

HttpManager.setTmpBaseUrl("http://192.168.0.127:8080/retrofitclientserver/");
HttpManager.get(UrlConfig.USER_INFO, null, callback);

如果需要临时添加请求头,需要在发送请求前调用:

HttpManager.addTmpHeaders(headers);
HttpManager.get(UrlConfig.USER_INFO, null, callback);

具体使用方法可以参考库中的 demo。

服务器端代码参考:RetrofitClientServer

License

MIT License

Copyright (c) [2017] [Jadyli]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Android 开发经验分享
用了一年多的理财推荐
Android 开发经验分享
用了一年多的理财推荐