如何发布带有两个字符串参数的zip文件

时间:2015-09-30 14:39:46

标签: android post retrofit

byte[] binary = FileUtils.readFileToByteArray(reportFile);
    TypedInput binInput = new TypedByteArray("application/zip", binary);
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(ContextConstants.REPORT_SUBMIT_PATH)
            .build();
    restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);
    http = restAdapter.create(CustomHTTPService.class);
    http.sendReport(stringBuffer.toString(), (userComments != null) ? userComments : "", binInput, new Callback<String>() {
        @Override
        public void success(String s, Response response) {
            if (response.getStatus() == 200) {

            }
        }

        @Override
        public void failure(RetrofitError error) {
            LOGGER.error("RetrofitError code: " + error.getMessage());
        }
    });

接口:

@FormUrlEncoded
@POST("/collectFeedback")
void sendReport(@Field("deviceInfo") String deviceInfo, @Field("garbageInfo") String garbageInfo,
                @Body TypedInput binary, Callback<String> response);

我遇到了这样的错误:

  

retrofit.RetrofitError:CustomHTTPService.sendReport:@Body参数不能与表单或多部分编码一起使用。 (参数#3)

1 个答案:

答案 0 :(得分:0)

这是我解决了我的问题:

@Multipart
@POST("/collectFeedback")
void sendReport(@Part("deviceInfo") TypedString  deviceInfo, @Part("garbageInfo") TypedString  garbageInfo,
                @Part("archive") TypedFile file, Callback<String> response);

接口的实现:

TypedString deviceInfo = new TypedString(stringBuffer.toString());
    TypedString garbageInfo = new TypedString((userComments != null) ? userComments : "");
    TypedFile archiveFile = new TypedFile("application/zip", reportFile);
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(ContextConstants.REPORT_SUBMIT_PATH)
            .build();
    http = restAdapter.create(CustomHTTPService.class);
    http.sendReport(deviceInfo, garbageInfo, archiveFile, new Callback<String>() {
        @Override
        public void success(String s, Response response) {
            LOGGER.info("response code: " + response.getStatus());
            if (response.getStatus() == 200) {
                boolean deleteResult = reportHelper.deleteCrashDumpFiles();
                LOGGER.info("DeleteFile Result: " + deleteResult);
            }
        }

        @Override
        public void failure(RetrofitError error) {
            LOGGER.error("RetrofitError code: " + error.getMessage());
        }
    });