android 使用Retrofit2 RxJava 文件上传

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);

  

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