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

版权声明:此BLOG为个人维护BLOG,内容均来自原创及互连网转载。最终目的为收集整理自己需要的文章技术等内容,不涉及商业用途。 本BLOG上原创文章未经本人许可,不得用于商业用途及传统媒体。网络媒体转载请注明出处,否则属于侵权行为。 3、有关本站侵权   本BLOG所转载的内容,均是本人未发现有对文章版权声明的文章且无来自传统媒体。如果本BLOG侵犯了您的权益,请联系本人,本人将会在第一时间删除侵权文章 https://blog.csdn.net/qq_20221151/article/details/52299004

本博客内容一致同步到本人的博客站点:http://www.zhoutaotao.xyz 欢迎访问留言交流

公司要求上传附件,接口那边实现的是文件数组接收,这边的话发送文件数组非常的不方便,所以研究了一下使用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());
            }
        });
    }

猜你喜欢

转载自blog.csdn.net/qq_20221151/article/details/52299004
今日推荐