0

Hi I want to send a json object and a list of image to a wcf web service I have declared Post function as below

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

In above code the List<RequestBody> is the image List. I write a function for call above function.

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

I don't know the above code for setting request body are true or not. When I call the web service I got 404 error, and If I declare the web service POST call as below I got the bad request error:

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

I don't know where is my mistake. please help me

BeginnerProgrammer
  • 664
  • 3
  • 13
  • 27

0 Answers0