该库是封装了okhttp的标准RESTful风格的网络框架,可以与RxJava完美结合,比Retrofit更简单易用。支持大文件上传下载,上传进度回调,下载进度回调,表单上传(多文件和多参数一起上传),链式调用,可以自定义返回对象,支持Https和自签名证书,支持超时自动重连,支持cookie与session的自动管理,支持四种缓存模式缓存网络数据,支持301、302重定向,扩展了统一的上传管理和下载管理功能。
implementation 'com.lzy.net:okgo:3.0.4'
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
OkGo.<String>get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1").execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String body = response.body();
Toast.makeText(MainActivity.this, ""+body, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Response<String> response) {
super.onError(response);
Log.i(TAG, "onError: "+response.body());
}
});
HashMap<String, String> map = new HashMap<>();
map.put("name","yyn");
map.put("password","1230");
OkGo.<String>post("https://www.apiopen.top/createUser?key=00d91e8e0cca2b76f515926a36db68f5&").params(map).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String body = response.body();
Toast.makeText(MainActivity.this, ""+body, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Response<String> response) {
super.onError(response);
Log.i(TAG, "onError: "+response.body());
}
});
OkGo.<File>get("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").execute(new FileCallback("/sdcard/Movies","xx.mp4") {
@Override
public void onSuccess(Response<File> response) {
Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Response<File> response) {
super.onError(response);
Log.i(TAG, "onError: "+response.body());
}
@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
pb.setProgress((int)progress.fraction*100);
}
});
OkGo.<String>post("http://10.1.3.199/ic/").isMultipart(true).params("file",new File("/sdcard/Movies/xx.mp4"),"xx.mp4").execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
}
@Override
public void uploadProgress(Progress progress) {
super.uploadProgress(progress);
//progress.fraction 获得当前上传进度 范围0-1
bar.setProgress((int) (progress.fraction*100));
}
});
OkGo.<Bitmap>get("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=486958716,243120904&fm=26&gp=0.jpg").execute(new BitmapCallback() {
@Override
public void onSuccess(Response<Bitmap> response) {
Bitmap bitmap = response.body();
iv.setImageBitmap(bitmap);
}
});
实现AbsCallback
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.lzy.okgo.callback.AbsCallback;
import okhttp3.ResponseBody;
public abstract class JsonCallBack<T> extends AbsCallback<T> {
private Class<T> clazz;
public JsonCallBack(Class<T> clazz) {
this.clazz = clazz;
}
@Override
public T convertResponse(okhttp3.Response response) throws Throwable {
ResponseBody body = response.body();
if(body!=null){
JsonReader jsonReader = new JsonReader(body.charStream());
T data = new Gson().fromJson(jsonReader, clazz);
return data;
}
return null;
}
}
OkGo.<GsonBean>get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1").execute(new JsonCallBack<GsonBean>(GsonBean.class) {
@Override
public void onSuccess(Response<GsonBean> response) {
GsonBean body = response.body();
Toast.makeText(MainActivity.this, body.toString(), Toast.LENGTH_SHORT).show();
}
});