第4天OKGO框架的使用
OKGO框架的依赖
implementation ‘com.lzy.net:okgo:3.0.4’
Glide的依赖
implementation ‘com.github.bumptech.glide:glide:4.6.1’
OKGO框架的使用代码
public class MainActivity extends AppCompatActivity {
ProgressBar bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar=findViewById(R.id.bar);
// getString();
// getBitmap();
// getFile();
// upload();
dopost();
}
//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();
}
});
}
//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请求json
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);
}
});
}
//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();
}
});
}
//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();
}
});
}
}
Glide的使用方法代码
ImageView imageView = findViewById(R.id.image1);
// Glide.with(this).load("http://169.254.113.244/hfs/zizi%20(1).jpg")
// .into(imageView);
// Glide.with(this).load("/sdcard/Pictures/pic.jpg")
// .into(imageView);
// Glide.with(this)
// .asGif()//判断该网址是不是gif动图
// .load("http://i.kinja-img.com/gawker-media/image/upload/s--B7tUiM5l--/gf2r69yorbdesguga10i.gif")
// .into(imageView);
//圆形头像
Glide.with(this)
.load("/sdcard/Pictures/QQ.jpg")
.apply(RequestOptions.bitmapTransform(new CircleCrop()))
.into(imageView);
//圆角
// Glide.with(this)
// .load("/sdcard/Pictures/QQ.jpg")
// .apply(RequestOptions.bitmapTransform(new RoundedCorners(100)))
// .into(imageView);
//默认显示图片
// final RequestOptions options = new RequestOptions().error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher_round);
//
// Glide.with(this)
// .load("http://169.254.113.244/hfs/zizi%20(1).jpg")
// .apply(options)
// .into(imageView);