OkGO的使用流程
1.在build.gradle中添加依赖
//okGo框架,封装了okhttp compile 'com.lzy.net:okgo:2.0.0' //可以单独使用,不需要依赖下方的扩展包 compile 'com.lzy.net:okrx:0.1.0' //RxJava扩展支持,根据需要添加 compile 'com.lzy.net:okserver:1.1.0'2.get请求
OkGo.get(url) .tag(this) .execute(new StringCallback() { @Override public void onSuccess(String s, Call call, Response response) { } });
3.post请求
不需要传值请求的
OkGo.post(url) .tag(this) .execute(new StringCallback() { @Override public void onSuccess(String s, Call call, Response response) { } });
需要传值请求的
OkGo.post(url) .tag(this) .params("","") .params("","") .execute(new StringCallback() { @Override public void onSuccess(String s, Call call, Response response) { } });
4.请求文件下载
OkGo.get(Urls.URL_DOWNLOAD)// .tag(this)// .execute(new FileCallback("file.jpg") { //文件下载时,需要指定下载的文件目录和文件名 @Override public void onSuccess(File file, Call call, Response response) { // file 即为文件数据,文件保存在指定目录 } @Override public void downloadProgress(long currentSize, long totalSize, float progress, long networkSpeed) { //这里回调下载进度(该回调在主线程,可以直接更新ui) } });5.基本网络请求
OkGo.get(Urls.URL_METHOD) // 请求方式和请求url .tag(this) // 请求的 tag, 主要用于取消对应的请求 .cacheKey("cacheKey") // 设置当前请求的缓存key,建议每个不同功能的请求设置一个 .cacheMode(CacheMode.DEFAULT) // 缓存模式,详细请看缓存介绍 .execute(new StringCallback() { @Override public void onSuccess(String s, Call call, Response response) { // s 即为所需要的结果 } });