OkHttp3的使用记录(下)

来源:互联网 发布:华为 网络管理 编辑:程序博客网 时间:2024/06/05 02:49

上一篇文章大概将OkHttp的使用的说完了OkHttp3的使用记录(上)


这篇将的是下载和上传

首先是上传

使用的是OkHttp的MultipartBody来构建RequestBody然后上传给服务器

upload方法

    public void upload() {        MultipartBody.Builder builder = new MultipartBody.Builder()                .setType(MultipartBody.FORM);        builder.addFormDataPart("title", "Square Logo")                .addFormDataPart("image", "logo-square.png",            RequestBody.create(MediaType.parse("image/png"), new File("website/static/logo-square.png")));        RequestBody requestBody = builder.build();        Request request = new Request.Builder()                .url("https://api.imgur.com/3/image")                .post(requestBody)                .build();        Call call = mOkHttpClient.newCall(request);                call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                Log.e("upload","失败");            }            @Override            public void onResponse(Call call, Response response) throws IOException {                Log.e("upload","成功");                Log.e("upload","body:"+response.body().string());            }        });    }

我们来看下是如何做到上传的

首先是构建了一个MultipartBody.Builder来创造RequestBody

在MultipartBody中使用添加addFormDataPart来添加要上传的内容


这边有两种形式 

第一种是上传了一个键值对

.addFormDataPart("title", "Square Logo")

键值对的构建就很像上篇文章里面的post键值对数据了。


第二个是上传的文件

.addFormDataPart("image", "logo-square.png",            RequestBody.create(MediaType.parse("image/png"), new File("website/static/logo-square.png")))

上传文件的话需要填写更多的东西了。

大概上传就是这么多了。


再次是下载

下载上一章也说过了其实就是以前的方式 获取到了is流 就是原来的操作了。

1.download方法

    public void download(final String url, final String path) {        Request request = new Request.Builder()                .url(url)                .build();        Call call = mOkHttpClient.newCall(request);        call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                downFile(response, url, path);            }        });    }

2.downFile方法

    private void downFile(Response response, String url, String path) {        InputStream is = null;        byte[] buf = new byte[2048];        int len = 0;        FileOutputStream fos = null;        try {            is = response.body().byteStream();            File file = new File(path, getName(url));            fos = new FileOutputStream(file);            while ((len = is.read(buf)) != -1) {                fos.write(buf, 0, len);            }            fos.flush();        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (is != null) {                    is.close();                }                if (fos != null) {                    fos.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }    }

3.getNmae方法

用来获取下载的是什么文件

    private String getName(String url) {        int index = url.lastIndexOf("/");        return (index < 0) ? url : url.substring(index + 1, url.length());    }

其实上面就是最基本的下载文件到固定路径的过程了。


至此,使用篇章就算记录了下!

然后一篇别人翻译的OkHttp官方wiki OkHttp 官方中文文档

1 0
原创粉丝点击