一. get请求
public void getString(){
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);
}
});
}
二.post请求
//post请求数据
public void dopost(){
HashMap<String, String> map = new HashMap<>();
map.put("phone","18233790750");
map.put("passwd","345");
OkGo.<String>post("https://www.apiopen.top/createUser?key=00d91e8e0cca2b76f515926a36db68f5&").params(map).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
Toast.makeText(MainActivity.this, ""+response.body(), Toast.LENGTH_SHORT).show();
}
});
}
三.下载文件显示进度条
//get下载文件
public void getFile(){
OkGo.<File>get("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
.execute(new FileCallback("/sdcard/","aaaaa.mp4") {
@Override
public void onSuccess(Response<File> response) {
File body = response.body();
}
@Override
public void onStart(Request<File, ? extends Request> request) {
super.onStart(request);
}
@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
//progress.fraction 获得当前上传进度 范围0-1
bar.setProgress((int) (progress.fraction*100));
}
@Override
public void onError(Response<File> response) {
super.onError(response);
}
@Override
public void onFinish() {
super.onFinish();
}
});
}
四.上传文件显示进度条
//post上传文件
private void upload() {
OkGo.<String>post("http://172.21.79.88/hfs/").isMultipart(true).params("file",new File("/sdcard/来自天堂的魔鬼.mp3"),"ass.mp3")
.execute(new StringCallback(){
@Override
public void onSuccess(Response<String> response) {
Toast.makeText(MainActivity.this, ""+response.body(), Toast.LENGTH_SHORT).show();
}
@Override
public void uploadProgress(Progress progress) {
super.uploadProgress(progress);
//progress.fraction 获得当前上传进度 范围0-1
bar.setProgress((int) (progress.fraction*100));
}
});
}
五.请求图片
//get请求图片
public void getBitmap(){
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();
Toast.makeText(MainActivity.this, ""+bitmap.getByteCount(), Toast.LENGTH_SHORT).show();
}
});
}
六.okGo简单的json封装
public abstract class JsonCallBack<T> extends AbsCallback<T> {
private Type type;
private Class clazz;
public JsonCallBack(Type type) {
this.type = type;
}
public JsonCallBack(Class t) {
this.clazz = t;
}
public JsonCallBack() {
}
/**
* 该方法是子线程处理,不能做ui相关的工作
* 主要作用是解析网络返回的 response 对象,生成onSuccess回调中需要的数据对象
*/
@Override
public T convertResponse(Response response) throws Throwable {
ResponseBody body = response.body();
if (body == null) return null;
T data = null;
Gson gson = new Gson();
String string = body.string();
if (type != null) data = (T) gson.fromJson(string , type);
if (clazz != null) data = gson.fromJson(string , clazz);
return data;
}
}
2.使用
OkGo.<Cai>get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1")
.execute(new JsonCallBack<Cai>(Cai.class) {
@Override
public void onSuccess(Response<Cai> response) {
Cai body = response.body();
Log.d("cai", "onSuccess: "+body);
}
});