当前位置: 首页 > 工具软件 > Go Glide > 使用案例 >

OkGo Glide

常彭薄
2023-12-01

OkGo

get请求,post请求,上传,下载,下载图片

package com.example.day1105okgo;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;

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;

public class MainActivity extends AppCompatActivity {

    private Button get;
    private Button post;
    private Button upload;
    private Button download;
    private SeekBar bar;
    private Button pic;
    private ImageView iv;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        get = (Button) findViewById(R.id.get);
        post = (Button) findViewById(R.id.post);
        upload = (Button) findViewById(R.id.upload);
        download = (Button) findViewById(R.id.download);
        bar = (SeekBar) findViewById(R.id.bar);
        pic = (Button) findViewById(R.id.pic);
        iv = (ImageView) findViewById(R.id.iv);




        //动态获取权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
        }

        //get请求
        get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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);
                                Toast.makeText(MainActivity.this, ""+response.getException().getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });

        //post请求
        post.setOnClickListener(new View.OnClickListener() {
            //limit=20&page=1
            @Override
            public void onClick(View v) {
                HashMap<String, String> map = new HashMap<>();
                map.put("limit","20");
                map.put("page","1");
                OkGo.<String>post("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&")
                        .params(map)
                        .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);
                                Toast.makeText(MainActivity.this, ""+response.getException().getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });


        //上传
        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkGo.<String>post("http://169.254.66.222/hfs/")
                        .isMultipart(true)
                        .params("file",new File("/sdcard/hahah.mp4"))
                        .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                    }

                            @Override
                            public void uploadProgress(Progress progress) {
                                super.uploadProgress(progress);
                                bar.setProgress((int) (progress.fraction*100));
                            }
                        });
            }
        });

        //下载
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkGo.<File>get("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4")
                        .execute(new FileCallback("/sdcard","aaaaaaaa.mp4") {
                    @Override
                    public void onSuccess(Response<File> response) {
                        Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
                    }

                            @Override
                            public void downloadProgress(Progress progress) {
                                super.downloadProgress(progress);
                                bar.setProgress((int) (progress.fraction*100));
                            }
                        });
            }
        });


        //下载图片
        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    OkGo.<Bitmap>get("http://p1.music.126.net/yC_df5u0myXVp-bM99K3Lw==/5870292580832850.jpg").execute(new BitmapCallback() {
                        @Override
                        public void onSuccess(Response<Bitmap> response) {
                            Bitmap body = response.body();
                            iv.setImageBitmap(body);
                        }
                    });
            }
        });

    }
}

OkGo二次封装

package com.example.app2.callback;

import android.util.Log;

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();
        if (body == null){
            return null;
        }
        String s = body.string();
        Gson gson = new Gson();
        if (tClass == null){
            return null;
        }
        T t = gson.fromJson(s,tClass);
        Log.i("ludan", "convertResponse: "+t);
        return t;
    }
}

Main

package com.example.app2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.app2.callback.Jsoncallback;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button xia;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xia = (Button) findViewById(R.id.xia);


        xia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkGo.<DouBean>get("https://www.apiopen.top/satinApi?type=1&page=2").execute(new Jsoncallback<DouBean>(DouBean.class) {
                    @Override
                    public void onSuccess(Response<DouBean> response) {
                        DouBean body = response.body();
                        int code = body.getCode();
                        Toast.makeText(MainActivity.this, ""+code, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }
}

Glide

package com.example.app3;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt1;
    private Button bt2;
    private Button bt3;
    private Button bt4;
    private Button bt5;
    private Button bt6;
    private Button bt7;
    private ImageView iv;


    private String image_url = "http://p1.music.126.net/yC_df5u0myXVp-bM99K3Lw==/5870292580832850.jpg";
    private String video_url = "http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4";
    private String image_path = "/sdcard/Pic1105.jpg";
    private String video_path = "/sdcard/shipin.mp4";
    private String gif_url = "http://i.kinja-img.com/gawker-media/image/upload/s--B7tUiM5l--/gf2r69yorbdesguga10i.gif";
    private Button bt8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        bt1 = (Button) findViewById(R.id.bt1);
        bt2 = (Button) findViewById(R.id.bt2);
        bt3 = (Button) findViewById(R.id.bt3);
        bt4 = (Button) findViewById(R.id.bt4);
        bt5 = (Button) findViewById(R.id.bt5);
        bt6 = (Button) findViewById(R.id.bt6);
        bt7 = (Button) findViewById(R.id.bt7);
        iv = (ImageView) findViewById(R.id.iv);

        //动态获取权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        }

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);
        bt7.setOnClickListener(this);

        bt8 = (Button) findViewById(R.id.bt8);
        bt8.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:
                Glide.with(this).load(image_path).into(iv);
                break;
            case R.id.bt2:
                Glide.with(this).load(video_path).into(iv);
                break;
            case R.id.bt3:
                Glide.with(this).load(image_url).into(iv);
                break;
            case R.id.bt4:
                Glide.with(this).load(video_url).into(iv);
                break;
            case R.id.bt5:
                Glide.with(this).load(gif_url).into(iv);
                break;
            case R.id.bt6:
                Glide.with(this)
                        .load(image_url)
                        .apply(new RequestOptions().bitmapTransform(new CircleCrop()))
                        .into(iv);
                break;
            case R.id.bt7:
                Glide.with(this)
                        .load(image_url)
                        .apply(new RequestOptions().bitmapTransform(new RoundedCorners(40)))
                        .into(iv);
                break;
            case R.id.bt8:
                Glide.get(this).clearMemory();//清理内存
                //清理磁盘是耗时操作
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.get(MainActivity.this).clearDiskCache();
                    }
                }).start();
                break;
        }
    }
}

 类似资料:

相关阅读

相关文章

相关问答