Android Studio 2 -4 OkGo 与 Glide
- OkGo 基本应用
- get请求+post请求+下载文件更新进度条+上传文件更新进度条+请求图片
- OkGo 的 json的二次封装:JsonCallBack
- Glide框架
- 一.Bitmap三级缓存机制
- 二. 加载本地SD卡图片+网络图片+资源图片+视频+GIF动图
- 三.加载圆形头像
- 四.加载圆角头像
- 五,设置占位符和加载失败图片
OkGo 基本应用
get请求+post请求+下载文件更新进度条+上传文件更新进度条+请求图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GET" />
<Button
android:id="@+id/btn_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="POST" />
<Button
android:id="@+id/btn_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载" />
<SeekBar
android:id="@+id/seek_down"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上传" />
<SeekBar
android:id="@+id/seek_up"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取图片" />
<SeekBar
android:id="@+id/seek_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
package com.example.day04ex;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.BitmapCallback;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import java.io.File;
import java.util.HashMap;
import okhttp3.MediaType;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private String upUrl = "http://169.254.142.167/hfs/";
private String getUrl = "http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&itemid=2&act=ad_app";
private String postUrl = "http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&";
private String mp4Url = "http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4";
private String picUrl = "http://img3.imgtn.bdimg.com/it/u=455940443,929091310&fm=26&gp=0.jpg";
private ImageView image;
private Button btn_get, btn_post, btn_up, btn_down, btn_iamge;
private SeekBar seek_down, seek_up, seek_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] permission = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permission, 1);
} else {
Toast.makeText(this, "请获取必要的权限,避免使用体验不良", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
}
}
private void initView() {
btn_get = (Button) findViewById(R.id.btn_get);
btn_post = (Button) findViewById(R.id.btn_post);
btn_up = (Button) findViewById(R.id.btn_up);
btn_down = (Button) findViewById(R.id.btn_down);
btn_iamge = (Button) findViewById(R.id.btn_image);
seek_down = (SeekBar) findViewById(R.id.seek_down);
seek_up = (SeekBar) findViewById(R.id.seek_up);
seek_image = (SeekBar) findViewById(R.id.seek_image);
image = (ImageView) findViewById(R.id.image);
btn_get.setOnClickListener(this);
btn_post.setOnClickListener(this);
btn_up.setOnClickListener(this);
btn_down.setOnClickListener(this);
btn_iamge.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_get:
OkGo.<String>get(getUrl).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String json = response.body();
Toast.makeText(MainActivity.this, "" + json, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Response<String> response) {
super.onError(response);
Toast.makeText(MainActivity.this, "" + response.message(), Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.btn_post:
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("itemid", "2");
hashMap.put("act", "ad_app");
OkGo.<String>post(postUrl).params(hashMap).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String json = response.body();
Toast.makeText(MainActivity.this, "" + json, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Response<String> response) {
super.onError(response);
Toast.makeText(MainActivity.this, "" + response.message(), Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.btn_up:
OkGo.<String>post(upUrl).params("file",new File("/sdcard/Music/Ice Paper - 心如止水.flac"),"sishui.mp3", MediaType.parse("media/mp3"))
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
}
});
break;
case R.id.btn_down:
OkGo.<File>get(mp4Url).execute(new FileCallback("/sdcard/Movies", "jsy.mp4") {
@Override
public void onSuccess(Response<File> response) {
}
@Override
public void onError(Response<File> response) {
super.onError(response);
}
@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
float fraction = progress.fraction;
Log.i(TAG, "downloadProgress: ");
seek_up.setProgress((int) (fraction * 100));
}
});
break;
case R.id.btn_image:
OkGo.<Bitmap>get("http://b-ssl.duitang.com/uploads/item/201804/24/20180424220005_MJLwz.png").execute(new BitmapCallback() {
@Override
public void onSuccess(Response<Bitmap> response) {
Bitmap body = response.body();
image.setImageBitmap(body);
}
@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
float fraction = progress.fraction;
Log.i(TAG, "downloadProgress: ");
seek_image.setProgress((int) (fraction * 100));
}
});
break;
default:
}
}
}
OkGo 的 json的二次封装:JsonCallBack
package com.example.day04ex;
import com.google.gson.Gson;
import com.lzy.okgo.callback.AbsCallback;
import okhttp3.Response;
import okhttp3.ResponseBody;
public abstract class JsonCallBack<T> extends AbsCallback<T> {
private Class<T> tClass;
public JsonCallBack(Class<T> tClass) {
this.tClass = tClass;
}
@Override
public T convertResponse(Response response) throws Throwable {
ResponseBody body = response.body();
String json = body.string();
Gson gson = new Gson();
T t = gson.fromJson(json, tClass);
return t;
}
}
package com.example.day04ex;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
public class JsonActivity extends AppCompatActivity {
private String getUrl = "http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&itemid=2&act=ad_app";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
OkGo.[]>get(getUrl).execute(new JsonCallBack<Bean[]>(Bean[].class) {
@Override
public void onSuccess(Response<Bean[]> response) {
Bean[] beans = response.body();
for (int i = 0; i < beans.length; i++) {
Log.i("jsy", "onSuccess: "+beans[i]);
}
}
});
}
}
package com.example.day04ex;
public class Bean {
private String aid;
private String title;
private String siteid;
private String userid;
private String pid;
private String typeid;
private String areaid;
private String amount;
private String currency;
private String url;
private String introduce;
private String hits;
private String username;
private String addtime;
private String editor;
private String edittime;
private String fromtime;
private String totime;
private String stat;
private String note;
private String code;
private String text_name;
private String text_url;
private String text_title;
private String text_style;
private String image_src;
private String image_url;
private String image_alt;
private String flash_src;
private String flash_url;
private String flash_loop;
private String key_moduleid;
private String key_catid;
private String key_word;
private String key_id;
private String listorder;
private String status;
private String alt;
private String thumb;
private String linkurl;
public String getAid() {
return aid;
}
public void setAid(String aid) {
this.aid = aid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSiteid() {
return siteid;
}
public void setSiteid(String siteid) {
this.siteid = siteid;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getTypeid() {
return typeid;
}
public void setTypeid(String typeid) {
this.typeid = typeid;
}
public String getAreaid() {
return areaid;
}
public void setAreaid(String areaid) {
this.areaid = areaid;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public String getHits() {
return hits;
}
public void setHits(String hits) {
this.hits = hits;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddtime() {
return addtime;
}
public void setAddtime(String addtime) {
this.addtime = addtime;
}
public String getEditor() {
return editor;
}
public void setEditor(String editor) {
this.editor = editor;
}
public String getEdittime() {
return edittime;
}
public void setEdittime(String edittime) {
this.edittime = edittime;
}
public String getFromtime() {
return fromtime;
}
public void setFromtime(String fromtime) {
this.fromtime = fromtime;
}
public String getTotime() {
return totime;
}
public void setTotime(String totime) {
this.totime = totime;
}
public String getStat() {
return stat;
}
public void setStat(String stat) {
this.stat = stat;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getText_name() {
return text_name;
}
public void setText_name(String text_name) {
this.text_name = text_name;
}
public String getText_url() {
return text_url;
}
public void setText_url(String text_url) {
this.text_url = text_url;
}
public String getText_title() {
return text_title;
}
public void setText_title(String text_title) {
this.text_title = text_title;
}
public String getText_style() {
return text_style;
}
public void setText_style(String text_style) {
this.text_style = text_style;
}
public String getImage_src() {
return image_src;
}
public void setImage_src(String image_src) {
this.image_src = image_src;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public String getImage_alt() {
return image_alt;
}
public void setImage_alt(String image_alt) {
this.image_alt = image_alt;
}
public String getFlash_src() {
return flash_src;
}
public void setFlash_src(String flash_src) {
this.flash_src = flash_src;
}
public String getFlash_url() {
return flash_url;
}
public void setFlash_url(String flash_url) {
this.flash_url = flash_url;
}
public String getFlash_loop() {
return flash_loop;
}
public void setFlash_loop(String flash_loop) {
this.flash_loop = flash_loop;
}
public String getKey_moduleid() {
return key_moduleid;
}
public void setKey_moduleid(String key_moduleid) {
this.key_moduleid = key_moduleid;
}
public String getKey_catid() {
return key_catid;
}
public void setKey_catid(String key_catid) {
this.key_catid = key_catid;
}
public String getKey_word() {
return key_word;
}
public void setKey_word(String key_word) {
this.key_word = key_word;
}
public String getKey_id() {
return key_id;
}
public void setKey_id(String key_id) {
this.key_id = key_id;
}
public String getListorder() {
return listorder;
}
public void setListorder(String listorder) {
this.listorder = listorder;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public String getThumb() {
return thumb;
}
public void setThumb(String thumb) {
this.thumb = thumb;
}
public String getLinkurl() {
return linkurl;
}
public void setLinkurl(String linkurl) {
this.linkurl = linkurl;
}
@Override
public String toString() {
return "Bean{" +
"aid='" + aid + '\'' +
", title='" + title + '\'' +
", siteid='" + siteid + '\'' +
", userid='" + userid + '\'' +
", pid='" + pid + '\'' +
", typeid='" + typeid + '\'' +
", areaid='" + areaid + '\'' +
", amount='" + amount + '\'' +
", currency='" + currency + '\'' +
", url='" + url + '\'' +
", introduce='" + introduce + '\'' +
", hits='" + hits + '\'' +
", username='" + username + '\'' +
", addtime='" + addtime + '\'' +
", editor='" + editor + '\'' +
", edittime='" + edittime + '\'' +
", fromtime='" + fromtime + '\'' +
", totime='" + totime + '\'' +
", stat='" + stat + '\'' +
", note='" + note + '\'' +
", code='" + code + '\'' +
", text_name='" + text_name + '\'' +
", text_url='" + text_url + '\'' +
", text_title='" + text_title + '\'' +
", text_style='" + text_style + '\'' +
", image_src='" + image_src + '\'' +
", image_url='" + image_url + '\'' +
", image_alt='" + image_alt + '\'' +
", flash_src='" + flash_src + '\'' +
", flash_url='" + flash_url + '\'' +
", flash_loop='" + flash_loop + '\'' +
", key_moduleid='" + key_moduleid + '\'' +
", key_catid='" + key_catid + '\'' +
", key_word='" + key_word + '\'' +
", key_id='" + key_id + '\'' +
", listorder='" + listorder + '\'' +
", status='" + status + '\'' +
", alt='" + alt + '\'' +
", thumb='" + thumb + '\'' +
", linkurl='" + linkurl + '\'' +
'}';
}
}
Glide框架
一.Bitmap三级缓存机制
Glide.get(GlideActivity.this).clearMemory();
new Thread(new Runnable() {
@Override
public void run() {
Glide.get(GlideActivity.this).clearDiskCache();
}
}).start();
二. 加载本地SD卡图片+网络图片+资源图片+视频+GIF动图
Glide .with(this).load(地址).into(imageView);
三.加载圆形头像
Glide.with(this)
.load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562587162663&di=58b961a5726901f5d3b97f8924ddc189&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F10%2F20181110102045_jwqny.jpg")
.apply(RequestOptions.bitmapTransform(new CircleCrop()))
.into(imageView);
四.加载圆角头像
Glide.with(this)
.load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562587162663&di=58b961a5726901f5d3b97f8924ddc189&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F10%2F20181110102045_jwqny.jpg")
.apply(RequestOptions.bitmapTransform(new RoundedCorners(100)))
.into(imageView);
五,设置占位符和加载失败图片
Glide.with(this)
.load("http://hbimg.b0.upaiyun.com/9852feef26dcba3d807282aa5dff561b1e223de85b027-Tzv0x0_fw658")
.apply(RequestOptions.bitmapTransform(new RoundedCorners(20)).error(R.mipmap.error).placeholder(R.drawable.ic_launcher_background))
.into(iv);