码迷,mamicode.com
首页 > 移动开发 > 详细

android 使用Retrofit2 RxJava 文件上传

时间:2018-05-25 13:51:46      阅读:790      评论:0      收藏:0      [点我收藏+]

标签:rri   factory   parser   media   tip   debug   data   使用   ant   

private static void upload(final Context context, final int type, File logFile) {
        Map<String, RequestBody> map = new HashMap<>();
        if (logFile != null && logFile.length() > 0) {
            map.put(parseMapKey("file", logFile.getName()), parseRequestBody(logFile));
        }

        ApiFactory.getInstance().getApiService().uploadLog(map)
                .compose(RxSchedulers.<List<String>>combine())
                .subscribe(new RxSubscriber1<List<String>>() {
                    @Override
                    public void onError(ApiException e) {
                        LogUtil.w("upload onFailure: ");
                    }

                    @Override
                    public void onNext(List<String> filePaths) {
                        if (!CommonUtil.isEmpty(filePaths))//上传成功
                        {
                            LogUtil.w("upload succ,filePath:" + filePaths.get(0));
                            DebugHelp.PrintLine("upload succ,filePath:" + filePaths.get(0));
                        }
                        File dir;
                        if (type == UPLOAD_TYPE_FUNCTION) {
                            dir = LogCollectorUtility.getExternalDir(context, Constants.FUNCTION_DIR_NAME);
                        } else {
                            dir = LogCollectorUtility.getExternalDir(context, Constants.DIR_NAME);
                        }
                        FileToZip.recursionDeleteFile(dir);
                    }
                });
    }


    public static RequestBody parseRequestBody(File file) {
        return RequestBody.create(MediaType.parse("multipart/form-data"), file);
    }

    public static String parseMapKey(String key, String fileName) {
        return key + "\"; filename=\"" + fileName;
    }
/**
     * 上传日志
     *
     * @return
     */
    @Multipart
    @POST("v1/app/exception")
    Observable<ResponseBean<List<String>>> uploadLog(@PartMap Map<String, RequestBody> map);

  

android 使用Retrofit2 RxJava 文件上传

标签:rri   factory   parser   media   tip   debug   data   使用   ant   

原文地址:https://www.cnblogs.com/baiyi168/p/9087247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!