使用application / x-www-form-urlencoded使用改造上传文件

时间:2017-08-30 04:52:31

标签: android retrofit2

如何配置改造工作如下图

enter image description here

这是我的代码:

    // interface ImageUploadService

    @Multipart
    @POST("/api=upl_img_version_2&token={token}&img_cat=3&sum={sum}")
    Observable<ServerResponse> uploadAvatar(@Path("token") String token, @Path("sum") String sum,  @Part MultipartBody.Part file);
    ===========================================

    //File creating from selected URL
    File file = new File(path);

    // create RequestBody instance from file
    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

    // body part send to server
    MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);

    view.showLoadingDialog();
    imageUploadService.uploadAvatar(token, sum, body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<ServerResponse>() {
                        @Override
                        public void onCompleted() {
                            view.hideLoadingDialog();
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            view.hideLoadingDialog();
                        }

                        @Override
                        public void onNext(ServerResponse serverResponse) {
                            Log.d("aaaaa", serverResponse.toString());
                        }
                    })

然后使用burp suite捕获请求,它与ios(工作)有所不同,我对此没有任何想法或关键词。感谢

1 个答案:

答案 0 :(得分:0)

我刚刚找到了一个解决方案,希望帮助某人面对同样的问题,而不是删除这个问题。

这是工作代码:

interface ImageUploadService {
   //@Multipart
  @POST("/api=upl_img_version_2&token={token}&img_cat=3&sum={sum}")
  Observable<ServerResponse> uploadAvatar(@Path("token") String token, @Path("sum") String sum,  @Body RequestBody file); // @Part => @Body
}

///////////////////////////////
File file = new File(path);

// MediaType.parse("multipart/form-data")
RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
imageUploadService.uploadAvatar(token, sum, requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<ServerResponse>() {
                        @Override
                        public void onCompleted() {
                            view.hideLoadingDialog();
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            view.hideLoadingDialog();
                        }

                        @Override
                        public void onNext(ServerResponse serverResponse) {
                            Log.d("aaaaa", serverResponse.toString());
                        }
                    })