okhttp

对okhttp的封装类,okhttp见:https://github.com/apple317/HttpClientUtils

用法

  • Android Studio

    使用前,对于Android Studio的用户,可以选择添加:

    compile project(':HttpClientUtils')

注意

目前最新版本,提供了自定Callback,可以按照下面的方式,自行解析返回结果:

目前支持

  • 一般的get请求
  • 一般的post请求
  • 基于Http Post的文件上传(类似表单)
  • 文件下载/加载图片 not
  • 上传下载的进度回调 not
  • 支持session的保持
  • 支持自签名网站https的访问,提供方法设置下证书就行
  • 支持取消某个请求
  • 支持自定义Callback
  • 支持HEAD、DELETE、PATCH、PUT not

用法示例

GET请求

 void initData(){        initParameter();        mParams.put("appkey", "56065429");        mParams.put("sign", "AF24BF8A3F31D22D25422BCDD86AA322F43B5BAB");        BaseHttpClient.getOkClient(getApplicationContext()).sendGetRequest("http://api.dianping.com/v1/metadata/get_cities_with_deals", mParams, new HttpCallback() {            @Override            public void onSuccess(String content, Object object, String reqType) {            }            @Override            public void onFailure(Throwable error, String content, String reqType) {            }        });    }     protected  void initParameter(){        if (mParams == null) {            mParams = new BaseParams();        }else {            mParams.clear();        }    }

POST请求 提交一个Gson字符串到服务器端。

 initParameter();        mParams.put("os", "2");        mParams.put("device_id", "123871827312");        mParams.put("version", "1.1");        mParams.put("game", "dota");        BaseHttpClient.getOkClient(getApplicationContext()).sendPostRequest("http://apphttpurl.com/v1", mParams, new HttpCallback() {            @Override            public void onSuccess(String content, Object object, String reqType) {            }            @Override            public void onFailure(Throwable error, String content, String reqType) {            }        });

Post File

initParameter();        mParams.put("os", "2");        File file=new File("app.png");        try {            //logo 是文件上传定义的名词,根据服务端定义调整            mParams.put("logo", file, FileNameGenerator.getMIMEType(file));        } catch (FileNotFoundException e) {            e.printStackTrace();        }        BaseHttpClient.getOkClient(getApplicationContext()).sendPostRequest("http://apphttpurl.com/v1", mParams, new HttpCallback() {            @Override            public void onSuccess(String content, Object object, String reqType) {            }            @Override            public void onFailure(Throwable error, String content, String reqType) {            }        });

将文件作为请求体,发送到服务器。

Post表单形式上传文件

initParameter();        mParams.put("os", "2");        File file=new File("app.png");        try {            //logo 是文件上传定义的名词,根据服务端定义调整            mParams.put("logo", file, FileNameGenerator.getMIMEType(file));        } catch (FileNotFoundException e) {            e.printStackTrace();        }        BaseHttpClient.getOkClient(getApplicationContext()).sendPostRequest("http://apphttpurl.com/v1", mParams, new HttpCallback() {            @Override            public void onSuccess(String content, Object object, String reqType) {            }            @Override            public void onFailure(Throwable error, String content, String reqType) {            }        });

支持单个多个文件,addFile的第一个参数为文件的key,即类别表单中<input type="file" name="mFile"/>的name属性。

自定义CallBack

取消单个请求

未开放接口。正在实现。Call call=BaseHttpClient.getBaseClient(getApplicationContext()).sendGetRequest("http://api.dianping.com/v1/metadata/get_cities_with_deals", mParams, new HttpCallback() {            @Override            public void onSuccess(String content, Object object, String reqType) {            }            @Override            public void onFailure(Throwable error, String content, String reqType) {            }        }); call.cancel();### 根据tag取消请求目前对于支持的方法都添加了最后一个参数`Object tag`,取消则通过执行。第一个参数就是tag传入。BaseHttpClient.getBaseClient(getApplicationContext()).sendGetRequest("get","http://api.dianping.com/v1/metadata/get_cities_with_deals", mParams,new HttpCallback(){            @Override            public void onSuccess(String content, Object object, String reqType) {            }            @Override            public void onFailure(Throwable error, String content, String reqType) {            }        });例如:在Activity中,当Activity销毁取消请求:
比如,在请求中传入tag对象,销魂时候可以统一销毁同一tag对象访问@Overrideprotected void onDestroy(){    super.onDestroy();     //可以取消同一个tag的     BaseHttpClient.getBaseClient(getApplicationContext()).cancelTag("tag对象");}## 混淆

更多相关文章

  1. android 网络编程 HttpGet类和HttpPost类使用详解
  2. [Android]网络资源下载时断点续传的实现
  3. Android列表实现单选、多选、全选、取消、删除
  4. Android:网络编程及Internet应用
  5. Android(安卓)开源项目xUtils3使用教程
  6. Android初级教程三个Dialog对话框小案例
  7. Http的请求方式
  8. Android(安卓)WebView 常见的九个问题
  9. Android(安卓)studio中为app添加多语言支持

随机推荐

  1. sql 数据库还原图文教程
  2. sql server中的decimal或者numeric的精度
  3. SQL 时间类型的模糊查询
  4. mssql 指定字段编号sql语句
  5. MSSQL 游标使用 心得
  6. Sql语句与存储过程查询数据的性能测试实
  7. select * from sp_who的解决方案
  8. 删除重复记录,并且剩下一条
  9. SQL Server"错误 21002: [SQL-DMO]用户 *
  10. SQL Server提示"选定的用户拥有对象,所以