大家好,又见面了,我是你们的朋友全栈君。
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();
}
});
}
1.自定义回调接口
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);
}
});
————————————————
版权声明:本文为CSDN博主「_yao_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34178710/article/details/97628606
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/128530.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...