欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

OkGo

程序员文章站 2022-04-18 10:06:24
OKGO导入依赖implementation ‘com.lzy.net:okgo:3.0.4’一、get请求 getId.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { OkGo.get("http://www.qubaobei.com/ios/cf/dish_lis...

OKGO

导入依赖
implementation ‘com.lzy.net:okgo:3.0.4’

一、get请求

 getId.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();
                    }
                });
            }
        });

二、下载

   downloadId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkGo.<File>get("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").execute(new FileCallback("/sdcard/Movies", "new.mp4") {
                    @Override
                    public void onSuccess(Response<File> response) {
                        File body = response.body();
                        Toast.makeText(MainActivity.this, "" + body.getName() + "下载成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Response<File> response) {
                        super.onError(response);
                        String message = response.getException().getMessage();
                        Toast.makeText(MainActivity.this, "下载失败" + message, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void downloadProgress(Progress progress) {
                        super.downloadProgress(progress);
                        float fraction = progress.fraction;
                        progressId.setProgress((int) (fraction * 1000));
                    }
                });
            }
        });

三、上传

  uploadId.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                OkGo.<String>post("http://117.51.155.210/hfs/")
                        .params("file", new File("/sdcard/a.jpg"))
                        .params("file", new File("/sdcard/im.jpg"))
                        .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);
                                float fraction = progress.fraction;
                                progressId.setProgress((int) (fraction * 1000));
                            }
                        });
            }
        });

本文地址:https://blog.csdn.net/Sun_Sees_You/article/details/107392519

相关标签: java android studio