如何通过改装将JsonObject和请求主体列表发送到WCF Web服务?

时间:2019-01-13 19:01:38

标签: android retrofit2 multipartform-data

嗨,我想将json对象和图像列表发送到wcf Web服务,我已经声明了Post函数,如下所示:

@Multipart
@POST(Post_Evaluation_Result)
Call<ResponseBody> sendEvalResultWithImages
(@Part("Username") RequestBody username,
 @Part("Password") RequestBody pass,@Part("evaluation") RequestBody jsonObject,
@Part("EvaluationDetailsImageList") List<RequestBody> EvaluationDetailsImageList);

在上面的代码中,List<RequestBody>是图像列表。我写了一个函数来调用上面的函数。

   public void sendImages(List<EvaluationDetail> evaluationDetails, JSONObject jsonObject, final ResultCallBack resultCallBack){
    ApiService apiService=AppSingleton.getInstance(context).getApiService();
    List<RequestBody> requestBodies=new ArrayList<>();
    RequestBody requestFile;
    for(int i=0;i<evaluationDetails.size();i++) {
        if(evaluationDetails.get(i).getImage()!=null) {
            File file = new File(evaluationDetails.get(i).getImage());
            if (file != null) {
                InputStream inputStream=null;
                byte[] buf=null;
                try {
                    inputStream = new FileInputStream(file);
                    buf = new byte[inputStream.available()];
                    while (inputStream.read(buf) != -1) ;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                requestFile = RequestBody.create(MediaType.parse("application/octet-stream"),buf);
                requestBodies.add(requestFile);
            }
        }
    }
    RequestBody username=RequestBody.create(MediaType.parse("text/html; charset=UTF-8"),"username");
    RequestBody password=RequestBody.create(MediaType.parse("text/html; charset=UTF-8"),"password");
    RequestBody json=RequestBody.create(MediaType.parse("application/json"),jsonObject.toString());
    if(questionIds!=null)
    apiService.sendEvalResultWithImages(username,password,json,requestBodies).enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            resultCallBack.onGetResult(null);
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            resultCallBack.onFailedGet();
        }
    });
}

我不知道上面用于设置请求正文的代码是否正确。当我调用Web服务时,出现404错误,并且如果我按如下所示声明Web服务POST调用,则会收到错误的请求错误:

@Multipart
    @POST(Post_Evaluation_Result)
    Call<ResponseBody> sendEvalResultWithImages
    (@Part("Username") String username,
     @Part("Password") String pass,@Part("evaluation") JsonObject jsonObject,
    @Part("EvaluationDetailsImageList") List<RequestBody> EvaluationDetailsImageList);

我不知道我的错误在哪里。请帮助我

0 个答案:

没有答案