get
需要导入okgo依赖
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
需要导入okgo依赖
HashMap<String,String> map=new HashMap<>();
map.put("limit","20");
map.put("page","1");
OkGo.<String>get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1").params(map).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
Toast.makeText(MainActivity.this, ""+response.body(), Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Response<String> response) {
super.onError(response);
}
});
下载
需要导入okgo依赖
OkGo.<File>get("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").execute(new FileCallback("/sdcard/Download","ccoo.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);
downloadBar.setProgress((int) (progress.fraction*100));
}
@Override
public void onError(Response<File> response) {
super.onError(response);
}
@Override
public void onFinish() {
super.onFinish();
}
});
上传
需要导入okgo依赖
OkGo.<String>post("http://10.1.7.210/Sound/").isMultipart(true).params("file",new File("/sdcard/Music/刘罡 _ 陈洁丽 - 超兽武装.mp3"),"ssr.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);
uploadBar.setProgress((int) (progress.fraction*100));
}
});
}
});
json封装
@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;
}