Android端利用RetroFit实现多文件上传

来源:互联网 发布:软件工资 编辑:程序博客网 时间:2024/05/22 17:07

公司要求上传附件,接口那边实现的是文件数组接收,这边的话发送文件数组非常的不方便,所以研究了一下使用RetroFit传递多文件上传。在这个上面学习的,写的很不错!http://blog.csdn.net/lmj623565791/article/details/51304204

 public void upfile() {        Log.e(">>>>>>>>>状态:", "开始上传.....");        File[] files = new File[3];        Log.e(">>>>>>>>>状态:", "载入文件.....");        String dir = Environment.getExternalStorageDirectory().toString();        files[0] = new File("/storage/emulated/0/appPhoto/android0.jpg");        files[1] = new File("/storage/emulated/0/appPhoto/android1.jpg");        files[2] = new File("/storage/emulated/0/appPhoto/android2.jpg");        Map<String, RequestBody> photos = new HashMap<>();        photos.put("files\";filename=\"Android_4.jpg", RequestBody.create(MediaType.parse("image/jpeg"), files[0]));        photos.put("files\";filename=\"Android_5.jpg", RequestBody.create(MediaType.parse("image/jpeg"), files[1]));        photos.put("files\";filename=\"Android_6.jpg", RequestBody.create(MediaType.parse("image/jpeg"), files[2]));        Retrofit retrofit = new Retrofit.Builder()                .baseUrl("http://192.168.8.126:8080/")                .addConverterFactory(GsonConverterFactory.create())                .addConverterFactory(ScalarsConverterFactory.create())                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())                .build();        upInterface upfile = retrofit.create(upInterface.class);        Call<upfileReturn> call = upfile.getReturn("55", "0", "多附件测试 from 安卓", "", "0", "0", "13", photos);        //call异步执行        call.enqueue(new Callback<upfileReturn>() {            @Override            public void onResponse(Call<upfileReturn> call, Response<upfileReturn> response) {                Log.e("数据成功:", "message: " + response.body().getMessage() + "\nmsg: " + response.body().getMsg());            }            @Override            public void onFailure(Call<upfileReturn> call, Throwable t) {                Log.e("数据失败:", t.toString());            }        });    }
0 0
原创粉丝点击