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

      

  • 相关阅读:
    如何修改mysql root密码
    【STL】list基础(转)
    时间控制timer settimeout setinterval
    刷新ArrayCollection的显示控件
    需要科普的网站
    flex 拖拽数据
    常用的资源网站
    as3 性能优化
    对象池
    Bitmap与Bitmapdata
  • 原文地址:https://www.cnblogs.com/baiyi168/p/9087247.html
Copyright © 2011-2022 走看看