zoukankan      html  css  js  c++  java
  • 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);
    

      

  • 相关阅读:
    BZOJ 3991 set维护dfs序
    BZOJ 4547 矩阵快速幂
    WERTYU | TEX Quotes
    高精度运算
    最大公约数和最小公倍数
    老鼠的旅行
    数据交换
    1136 A Delayed Palindrome
    1137 Final Grading
    1138 Postorder Traversal
  • 原文地址:https://www.cnblogs.com/baiyi168/p/9087247.html
Copyright © 2011-2022 走看看