retrofit 上传一张或多张图片复杂但很有效的方法

我项目请求网络用的retrofit,但在上传图片时用网上给的很多方式都实现不了,在上传多张图片时有的方式后台只能接收到最后一张,用map上传时后台直接接收不到,最后发现了一个笨重的方法:

                StringBuffer paramsData = new StringBuffer();
                //paramsData.append();
                MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
                builder.addFormDataPart("action", "ninini");
                builder.addFormDataPart("paramData", paramsData.toString());
                MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
                switch (selImageList.size()){
                    case 5:
                        File f4 = new File(selImageList.get(4).path);
                        builder.addFormDataPart("thumb5", f4.getName(), RequestBody.create(MEDIA_TYPE_PNG, f4));
                    case 4:
                        File f3 = new File(selImageList.get(3).path);
                        builder.addFormDataPart("thumb4", f3.getName(), RequestBody.create(MEDIA_TYPE_PNG, f3));
                    case 3:
                        File f2 = new File(selImageList.get(2).path);
                        builder.addFormDataPart("thumb3", f2.getName(), RequestBody.create(MEDIA_TYPE_PNG, f2));
                    case 2:
                        File f1 = new File(selImageList.get(1).path);
                        builder.addFormDataPart("thumb2", f1.getName(), RequestBody.create(MEDIA_TYPE_PNG, f1));
                    case 1:
                        File f = new File(selImageList.get(0).path);
                        builder.addFormDataPart("thumb1", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
                        break;
                }

                MultipartBody requestBody = builder.build();
                //构建请求
                Request request = new Request.Builder()
                        .url("")//地址
                        .post(requestBody)//添加请求体
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Looper.prepare();
                        Tos.t(mActivity, "上传失败");
                        Looper.loop();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Gson gson = new Gson();
                        NowShowBean nowShowBean = gson.fromJson(new String(response.body().string()), Bean.class);
                        if (nowShowBean.getStatus() == 1){
                            Looper.prepare();
                            Tos.t(mActivity,"上传成功");
                            Looper.loop();
                        }else{
                            Looper.prepare();
                            Tos.t(mActivity,"上传失败111");
                            Looper.loop();
                        }
                    }
                });

在上传时一般会先压缩一下,关于压缩图片,文件会用到一个第三方框架,地址见:http://www.jcodecraeer.com/a/opensource/2017/0413/7819.html

你可能感兴趣的:(android开发问题)